1

私は ajax リクエストを送信しており、その結果を div に追加しました。この追加後、追加されたリンクをクリックしたい場合 (exec jquery クリック関数)、なぜクリック関数が機能しないのですか? (下手な英語でごめんなさい:P)

編集:

jQuery('.more').live("click",function() {
    var ID = jQuery(this).attr("id");
    if(ID) {
        jQuery("#more"+ID).html('<img src="template/css/images/moreajax.gif" />');
    jQuery.ajax({
        type: "POST",
        url: "loadmore.php",
        data: "lastid="+ ID, 
        cache: false,
        success: function(html){
            $("#contentWall").append(html);
        jQuery("#more"+ID).remove(); // removing old more button
        }
    });
    } else {
        jQuery(".morebox").html('The End');// no results
    }
return false;
});
4

3 に答える 3

7

これを機能させるには、jQuery のバージョンに応じて、.live()またはを使用する必要があります。.on()

通常.click()は、現在 DOM にある要素にのみ適用され、将来の追加には適用されません。

于 2012-07-02T13:51:35.377 に答える
0

これ以上の情報がなければ確かに難しいですが、おそらくあなたの問題は、このようなものを使用していることです

// this only bind click handler to existing elements matching selector
$('#mycontainer a').click(function(){...}) 

onまた、動的に追加された要素をカバーするために使用する必要があります。

// this bind click handler to current and future elements matching selector
$('#mycontainer').on('click','a',(function(){...}) 
于 2012-07-02T13:51:49.077 に答える
0

When your page originally loads, your click event is bound to your links. This means that any new links introduced to the DOM after that don't have the click event bound to them. You need to bind the click event to the link you are appending:

$(newlink).click(function(event){
    // your onclick code
});
$(mydiv).append(newlink);
于 2012-07-02T13:52:53.537 に答える