English 中文(简体)
如何在 pjax 载荷前后运行 jQuery?
原标题:How to run jQuery before and after a pjax load?

我目前在使用 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;
});
最佳回答

正如他们在文件中所说,“https://github.com/defunkt/jquery-pjax><这里pjax 点燃了两件你需要的东西。

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

pjax: 启动 - 当 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 });
问题回答
$("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;
});




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签