【问题标题】:How to run jQuery before and after a pjax load?如何在 pjax 加载之前和之后运行 jQuery?
【发布时间】:2012-05-29 14:58:12
【问题描述】:

我目前正在使用 pjax,它运行良好,但我需要运行两个 jQuery 函数,一个在 pjax 加载新 url 之前,一个在加载新 url 之后,我该怎么做?

我尝试了以下两种变体,但似乎都不起作用?

第一次尝试:

$("a").pjax("#main").live('click', function(){

    //Function Before Load  
    // Function After Load
})  

第二次尝试:

$("body").on("click", "a", function(){

    //Function Before Load  

    $(this).pjax("#main");

    //Function After Load   

    return false;
});

【问题讨论】:

  • 您尝试过docs 中的内容吗?
  • @ChristianVarga 我错过了那部分,以为 jQuery 部分在移到服务器端后完成了。

标签: javascript jquery ajax pjax


【解决方案1】:

正如他们在文档中所说的那样 here pjax 会根据您的需要触发两个事件

pjax 将在您要求它加载的容器上触发两个事件 您的回复正文为:

pjax:start - 当一个 pjax ajax 请求开始时触发。

pjax:end - 当一个 pjax ajax 请求结束时触发。

还有这个的代码示例

$('a.pjax').pjax('#main')
$('#main')
  .on('pjax:start', function() { //do what you want to do before start })
  .on('pjax:end',   function() { //after the request ends });

【讨论】:

    【解决方案2】:
    $("body").on("click", "a", function(){
    
     $(this).pjax("#main");
     $('a.pjax').pjax('#main')
     $('#main').on('pjax:start', function() { /* your code before ajax load */ });
     $('#main').on('pjax:end',   function() { /* your code after ajax load */ });
    
      return false;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多