简体   繁体   English

如何处理附加内容? jQuery的

[英]How to manipulate appended content? Jquery

I'm with Jquery. 我在使用Jquery。

I appended html to a div like that: 我将html附加到这样的div上:

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

The createLettersHtml function creates a list of letters (links) all wrapped in 'div.ln-letters'. createLettersHtml函数创建所有包裹在“ div.ln-letters”中的字母(链接)列表。 Then I want to call a 'onclick' event like that: 然后,我想像这样调用“ onclick”事件:

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

    alert(123);

});

but nothing happens! 但是什么也没发生! If I will call onclick on any non-appended html element it works, but it doesn't work with appended content. 如果我要在任何未附加的html元素上调用onclick,则可以使用,但不适用于附加的内容。 What do I do wrong? 我做错了什么?

UPDATE: 更新:

Here is full code: 这是完整的代码:

$(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);

});

});

you can try using .live . 您可以尝试使用.live You can hook this up in doc ready. 您可以将其连接到文档中。 .live works well for appended content. .live适用于附加内容。

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

You have to set click evet AFTER you append HTML content. 附加HTML内容后,必须设置单击evet。 Are you sure you are doing that? 确定要这样做吗? Besides, use Firebug or Opera Dragonfly to check if you have any errors in you JS. 此外,使用Firebug或Opera Dragonfly检查JS中是否有任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM