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.

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

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目