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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?