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 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗