dou72260 2015-05-11 19:39
浏览 163
已采纳

在infinitescroll之后点击事件的Jquery无效

I'm using the cakephp framework and the jquery infinitescroll plugin for loading the content dynamically. And each post has a link with an ajax action.

When a load the page there are 5 posts and the ajax links are working fine. But when a load 5 other posts with the infitescroll (ajax) the ajax link is not working.

It looks that the ajax function cannot find the id of the link but I don't find a solution.

 <a href="/questions" onclick="return false"><i class="glyphicon glyphicon-circle-arrow-right" id="581617"></i></a>

<script type="text/javascript">
 $(document).on("click","#581617",function(){
        $.ajax({url: "the_url", 
                    type:'post',
           })   });
    </script>

UPDATE: After making changes based on @ZacharyKniebel's solution, I am trying to write my selector to attach to links by ID using the below, which is not working. Can anyone tell me why?

"a > i.glyphicon.<?php echo $linkId;?>"
  • 写回答

1条回答 默认 最新

  • dtvq4978 2015-05-11 19:55
    关注

    It looks like your delegating your handler just for the element with id #581617. Instead, try delegating your handler to more broadly look for your a > i.glyphicon.glyphicon-circle-arrow-right elements:

    $(document).on("click","a > i.glyphicon.glyphicon-circle-arrow-right",function(){
        $.ajax({
            url: "the_url", 
            type:'post'
        });   
    });
    

    As a side-note, in the above I also removed the trailing comma after type:'post' for sake of IE functionality. Although keeping the trailing comma is technically valid, IE doesn't like it very much.


    UPDATE:

    With regards to the selector that you are now trying to use, note that you can't have the selector work for all newly loaded links if you are selecting them by ID. There are workarounds that technically could do this, but they're incredible dirty and have awful performance. This is because IDs are unique and binding this handler by ID is like trying to bind to each link individually, instead of binding to all of the links at once. (Technically, since this is a delegated handler, you're not really binding to the links, but you are listening for them).

    In short, the selector that I gave you, above, is what you want to use to make your event handler listen for all of them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀