weixin_33738578 2012-04-04 18:47 采纳率: 0%
浏览 31

Ajax调用后Js无法正常工作

I have a slider on homepage, i am trying to load next five posts in it on click of a button using Ajax. When i use Jquery .post to load the html of next five posts, the slider disappears.The cause being that a script called 'slider.js' does not work on the newly added html. I tried using .live() but it didn't work, i can see the response from Ajax call in my console so i know tha call is working. I tried including the js file in Ajax response but it doesn't fire even then. The js file is a long file with a lot of functions, i cannot enclose it in one function as suggested by some people on the forum. How would i reload the slider.js to make it work? Here is the ajax .post() code.

$(function() { 

   $('.nextfive').live('click', function() { 

      var data = {action: 'next_five',next: 5};

      jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", data,

      function(response) {

             $('#navi').slideDown("slow");  
             $("#slidercontainer").html(response); 

          },"html");   

      }); 
}); 

Edit: check the page at http://axactsoft.com/samplewp/ and click the button on top 'click five'. The code of files is pasted here http://pastebin.com/4iS6Ey10

  • 写回答

2条回答 默认 最新

  • weixin_33722405 2012-04-04 20:14
    关注

    Try with this:

    $('.nextfive').on({ 
    
        click: function () {
    
                  var data = {action: 'next_five',next: 5};
    
                  $.post("<?php echo admin_url('admin-ajax.php'); ?>", data,
    
                  function (response) {
    
                     $('#navi').slideDown("slow");  
                     $("#slidercontainer").html(response);
                  }
         }    
    
    }, 'body'); 
    

    You must use jquery 1.7 or above to use the .on() function. It's hard to know what you're doing without seeing the HTML structure of all the div IDs you're using.

    I would also recommend splitting your code: you have an event handler that holds a function for ajax that itself nests a function to handle the ajax callback. I'd have the event handler handle the event and pass the parameter to a different function that handles just the ajax, and have that function call a function that handles just the processing of the ajax response. You should 3 functions in the same scope instead of having 3 functions nested into each other.

    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误