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

Javascript 如何在pjax加载前后运行jQuery?,javascript,jquery,ajax,pjax,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", functio

我目前正在使用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;
});
正如他们在他们的doc
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 });

你试过了吗?@ChristianVarga我错过了这一部分,虽然jQuery部分在移到服务器端后就完成了。
$("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;
});