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

Dan*_*Dan 8 javascript ajax jquery pjax

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

我尝试了以下两种变化,但似乎都没有效果?

第一次尝试:

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

    //Function Before Load  
    // Function After Load
})  
Run Code Online (Sandbox Code Playgroud)

第二次尝试:

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

    //Function Before Load  

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

    //Function After Load   

    return false;
});
Run Code Online (Sandbox Code Playgroud)

Pra*_*Nag 25

当他们在他们的医生说这里 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 });
Run Code Online (Sandbox Code Playgroud)