gpt4 book ai didi

javascript - 如何操作 append 内容? jQuery

转载 作者:行者123 更新时间:2023-11-28 14:06:05 26 4
gpt4 key购买 nike

我使用 Jquery。

我将 html append 到这样的 div 中:

$('div#byletter').append(createLettersHtml());

createLettersHtml 函数创建所有包含在“div.ln-letters”中的字母(链接)列表。然后我想调用一个像这样的“onclick”事件:

$('.ln-letters a').click(function(){

alert(123);

});

但是什么也没发生!如果我对任何非 append 的 html 元素调用 onclick,它会起作用,但它不适用于 append 内容。我做错了什么?

更新:

完整代码如下:

$(document).ready(function(){

$.fn.listnav = function(options) {
var opts = $.extend({}, $.fn.listnav.defaults, options);
var letters = ['_', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '-'];
var firstClick = false;
opts.prefixes = $.map(opts.prefixes, function(n) { return n.toLowerCase(); });

$('div#byletter').append(createLettersHtml());

function createLettersHtml() {
var html = [];
for (var i = 1; i < 27; i++) {
if (html.length == 0) html.push('<a class="all" href="#">ALL</a><a class="_" href="#">0-9</a>');
html.push('<a class="' + letters[i] + '" href="#">' + ((letters[i] == '-') ? '...' : letters[i].toUpperCase()) + '</a>');
}
return '<div class="ln-letters">' + html.join('') + '</div>' + ((opts.showCounts) ? '<div class="ln-letter-count" style="display:none; position:absolute; top:0; left:0; width:20px;">0</div>' : ''); // the styling for ln-letter-count is to give us a starting point for the element, which will be repositioned when made visible (ie, should not need to be styled by the user)
}



};

$.fn.listnav.defaults = {
initLetter: '',
includeAll: true,
incudeOther: false,
includeNums: true,
flagDisabled: true,
noMatchText: 'No matching entries',
showCounts: true,
cookieName: null,
onClick: null,
prefixes: []
};


$('.ln-letters a').click(function(){

alert(123);

});

});

最佳答案

您可以尝试使用.live 。您可以将其连接到准备好的文档中。 .live 对于 append 内容效果很好。

$(function(){
$('div.ln-letters a').live('click', function(){
alert(123);
});
});

关于javascript - 如何操作 append 内容? jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1283800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com