dongtu1958 2013-11-17 15:09
浏览 44
已采纳

在加载动态php内容之前运行Jquery / Ajax

I've got a dynamic page which I'm loading from an external PHP file when the booking link is clicked.

My issue is the jQuery code I need to run straight after it sometimes tries to execute before the PHP page is fully loaded (the follow and remove parts specifically). So when I try load the div, jQuery works sometimes, and other times not.

From what I've seen on other examples $(document).ready(function() is supposed to prevent the jQuery executing until the page is fully loaded, however since I'm loading a specific div (content) and not the entire page it seems to not work?

$(document).ready(function(){
 $(document).on('click', '.booking', function (){
  $('#content').load('calendar.php');

     $.getJSON(url,data, function(data) {
     $.each(data, function(index, data) {


  $("#blocksid"+data.slot_id).css("background-color","#009cd0");
      $("#follow"+data.slot_id).hide();
  $("#remove"+data.slot_id).show();       
});

 });
  return false;
});
});

Thanks in advance for any assistance!

  • 写回答

2条回答 默认 最新

  • douzui0143 2013-11-17 15:22
    关注

    To achieve what you are expecting in a predicable way you should run dependent javascript code after loading content to div. Just create a call-back function for load method and have your dependent code inside it.

    $('#content').load("calendar.php", function() {
      //put the code here, that you need to run after loading content to above div
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?