簡體   English   中英

通過Ajax加載的jQuery動態內容

[英]jQuery dynamic content loaded via Ajax

嘿,我正在嘗試關閉在頁面加載時顯示的div。 但是,這樣做:

jQuery(document).ready(function () {
   jQuery('#moreDetailsTable').slideUp("fast", function () {});
});

頁面加載時似乎沒有關閉它,因為它是使用ajax通過PHP代碼動態填充的。

Ajax調用的php如下所示:

echo '<div id="moreDetailsTable" class="widefat fixed comments">
    Testing this out<br />
    Testing this out<br />
    Testing this out<br />
    Testing this out<br />
    Testing this out<br />
</div>
   ect ect.....

我知道代碼的工作原理,因為我只是這樣做:

jQuery(document).on('click', '#moreDetailsTable', function() {
    jQuery('#moreDetailsTable').slideUp("fast", function () {
        console.log('done');
    });
});

一旦我單擊它 ,它就會向上滑動。

由於.slideUp是動態填充的,而不是在頁面開頭,我怎么稱呼它呢?

請參閱jQuery.ajaxComplete()事件處理程序。 這將做您想要的。

例:

jQuery(document).ajaxComplete(function () {
   jQuery('#moreDetailsTable').slideUp("fast", function () {});
});

當ajax返回時調用該函數。 加載內容后。 就像是:

$('.ajaxLoading').show();
    $.ajax({
    type:'POST',
    url:'url',
    dataType: "json"}).done
    (function(response){
        alert(response.message);
        $('.ajaxLoading').hide();
        //window.location = "/dash_board";
            $('#moreDetailsTable').slideUp("fast", function () {});
    }).fail(function(response,textStatus){
        alert('Something goes wrong. Please try again.');
        $('.ajaxLoading').hide();
        //alert(textStatus);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM