dsxz84851 2014-04-04 13:02 采纳率: 100%
浏览 50
已采纳

AJAX停止加载相同的站点不同的PHP ID

So i have a website that I'm doing for a school project. It's supposed to be like PasteBin. So on the right theres a different div (Uued koodid) that shows newest pastes. On click, they are supposed to show what they include using AJAX to refresh the left div. This only works for 4 times and then stops, but URL is still changing. After refresh it changes again and works again for 4 more times.

In main.js i have

...
$.ajaxSetup({ cache: false });
...
$(".uuedKoodid").click(function () {
    $(".left-content").load(document.location.hash.substr(1));
});
...

EDIT: Also other AJAX functions work. If I log in, I can switch between settings and profile perfectly but still cannot watch new codes

  • 写回答

1条回答 默认 最新

  • doumiyi7063 2014-04-04 13:30
    关注

    When you replace right menu with new code (from ajax call) you don't attach click event again on .uuedKoodid items so they don't do anything. You need to attach event again or attach it like this:

    $(document).on('click', '.uuedKoodid', function () {
      $(".left-content").load(document.location.hash.substr(1));
    });
    

    Edit: As you noticed this will cause small problem. onclick event run before browser run standard link action. First you load ajax and then browser changes address. This way you are 1 action behind. Better solution than reading with delay (setTimeout) i think would be to read address directly from link:

    $(document).on('click', '.uuedKoodid', function () {
      var url = $(this).attr('href');
      $(".left-content").load(url.substring(url.indexOf("#")+1));
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。