在模态窗口中打开WordPress博客

问题描述:

我有下面的代码,它打开一个Ajax模式内的Wordpress文章,似乎工作正常。在模态窗口中打开WordPress博客

问题是,我也希望帖子导航能够在模式中工作,但它看起来像jquery代码一旦运行一次就会变得过时。

有没有什么办法让代码在运行后仍能正常工作?看起来模式内的帖子内容忽略了onclick的代码。

希望是有道理的,

(function($) { 
jQuery(document).ready(function($){ 
    //$.ajaxSetup({cache:false}); 
    $(".esg-grid a, .postmodal a").click(function(event){ 
      event.preventDefault(); 
     var post_url = $(this).attr("href"); 
     var post_id = $(this).attr("rel"); 

      //$(".postmodal").load(post_url); 
       $(".postmodal").load(post_url + " #main-content"); 
       $(".postmodal-container").removeClass("hidden"); 
       //window.history.pushState("object or string", "Title", "/new-url"); 
    return false; 
    }); 
}); 
})(jQuery); 

click事件不会对动态加载环节的工作,因为他们的document.ready一刻都没有了。您需要使用委托事件处理:

$(document).on("click", ".esg-grid a, .postmodal a", function(event) { 
    // the body of your callback 
}); 
+0

完美!谢谢:) –

+0

有没有一种简单的方法,当点击导航链接时,我可以使模态滚动到顶部? –