如何在pjax加载之前和之后运行jQuery? [英] How to run jQuery before and after a pjax load?

查看:112
本文介绍了如何在pjax加载之前和之后运行jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently using pjax and it works great but I need to run two jQuery functions, one before pjax loads the new url and one after the new url is loaded, how can I do this?

我尝试了以下两种变体,但似乎都没有用?

I have tried the following two variations but neither seem to work?

首次尝试:

$("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;
});


推荐答案

正如他们在文档中所说的其他 pjax 根据需要触发两个事件

As they says in their doc here pjax fires two events for what you need


pjax会在容器上触发两个事件,你要求它将你的响应体加载到

pjax will fire two events on the container you've asked it to load your reponse body into:

pjax:start - 在pjax ajax请求开始时触发。

pjax:start - Fired when a pjax ajax request begins.

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

pjax:end - Fired when a pjax ajax request ends.

这个代码示例

$('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 });

这篇关于如何在pjax加载之前和之后运行jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆