簡體   English   中英

如何簡化示例中的jQuery代碼?

[英]How to simplify the jQuery code in the example?

有一個用於AJAX的loada頁面的插件https://github.com/defunkt/jquery-pjax問題是它僅更新頁面上的一個容器,並且需要幾個。 以下代碼有效,但已棄用(第二個容器被延遲更新)。 如何優化同時加載的容器?

$(document).on("click", "a.item-link", (function (evt) {
      evt.preventDefault();
      var thisUrl = $(this).attr('href');
      $.pjax({
          url: thisUrl,
          container: '.pjax',
          fragment: '.pjax'
      });
      setTimeout(function() {
        $.pjax({
          url: thisUrl,
          container: '.box-mnu',
          fragment: '.box-mnu'
        });
      }, 1000);
  }));

嘗試使用.load() ,刪除setTimeout調用

  $(document).on("click", "a.item-link", (function (evt) {
      evt.preventDefault();
      var thisUrl = $(this).attr('href');
      $(".pjax").load(thisUrl);
      $(".box-mnu").load(thisUrl);
  });

在代碼中刪除setTimeout調用。

$(document).on("click", "a.item-link", (function (evt) {
      evt.preventDefault();
      var thisUrl = $(this).attr('href');
      $.pjax({
         url: thisUrl,
         container: '.pjax',
         fragment: '.pjax'
      });
      $.pjax({
         url: thisUrl,
         container: '.box-mnu',
         fragment: '.box-mnu'
      });
}));
$(document).pjax('.pjax-link', '.first-container', {fragment: '.first-container'});
$('.pjax').on('pjax:success', function () {
    $.pjax({
        url: window.location.href,
        container: '.second-container',
        fragment: '.second-container'
    });
});

暫無
暫無

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

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