开发者

Class click won't trigger

http://jsfiddle.net/yTkTd/

Another function with a class works perfectly, but this one seems to be giving me trouble. No console errors or anythi开发者_高级运维ng, it just won't trigger.

EDIT: NEW JSFIDDLE http://jsfiddle.net/yTkTd/9/


Place a .delegate() handler on #showList so it will process the clicks for the dynamically created .show elements.

Here's your updated example: http://jsfiddle.net/yTkTd/11/

$('#showList').delegate('.show', 'click', function() {
    showID = $(this).attr("id");
    $(".show").fadeOut("slow");
    $(this).fadeIn("slow");
    $.getJSON('https://api.phish.net/api.js?api=2.0&method=pnet.shows.setlists.get&format=json&apikey=A9E68C2561D7442F041C&callback=?&showid='+showID, function pnet3setlist(data) {
        $('#showList').append("<div>");
        for (i = 0; i < data.length; i++) {
            var n = data[i];
            $('#setList').append("<a class='show' href='#' id='setlist"+n['showid']+"'>"+n['setlistdata']+"</a>");
        }
        $('#showList').append("</div>");
    });
});


The .show elements are added dynamically, after your click handler is attached. You should use .live() instead:

$(".show").live("click", function() {
    alert('hello');
});

This will behave the same, but causes jQuery to listen for click events at the document level (which is always there), but checks to see if the source element is a ".show".


Change the framework to jQuery. Currently Mootools is your library.


you had JSFiddle set to use mootools while you were using jQuery. I updated and ran and the click works fine... http://jsfiddle.net/yTkTd/6/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜