如何在pjax加载之前和之后运行jQuery?

时间:2012-05-23 04:41:54

标签: javascript jquery ajax pjax

我目前正在使用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;
});

2 个答案:

答案 0 :(得分:25)

正如他们在他们的文档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 });

答案 1 :(得分: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;
});
相关问题