委托函数在jquery 2.1.1中不起作用

时间:2016-10-25 08:39:31

标签: javascript jquery html jquery-2.0

我在comment-list div中使用jQuery delegate进行分页的加载内容代码:

JS:

<script type="text/javascript">
    $('#comment-list .pagination a').delegate('click', function() {
        $('#comment-list').fadeOut('slow');

        $('#comment-list').load(this.href);

        $('#comment-list').fadeIn('slow');

        return false;
    });         

    $('#comment-list').load('index.php?route=simple_blog/article/comment&simple_blog_article_id=<?php echo $simple_blog_article_id; ?>');

</script>

HTML:

<div id="comment-list"> 
  <div class="pagination">
    <div class="links">
     <b>1</b> 
       <a href="http://localhost/oc/index.php?route=simple_blog/article/comment&amp;simple_blog_article_id=5&amp;page=2">2</a> 
       <a href="http://localhost/oc/index.php?route=simple_blog/article/comment&amp;simple_blog_article_id=5&amp;page=3">3</a>
       <a href="http://localhost/oc/index.php?route=simple_blog/article/comment&amp;simple_blog_article_id=5&amp;page=2">&gt;</a>
       <a href="http://localhost/oc/index.php?route=simple_blog/article/comment&amp;simple_blog_article_id=5&amp;page=3">&gt;|</a> </div><div class="results">Showing 1 to 5 of 11 (3 Pages)
   </div>
  </div>
</div>

但是在行动中我的代码不起作用而不在div中加载内容。点击分页链接后,我看到打开新窗口并加载我的内容。我在jQuery 2.1.1版本中测试了我的代码,我认为delegate无效jQuery 2.1.1

如何解决我的问题?

1 个答案:

答案 0 :(得分:2)

  

从jQuery 3.0开始,.delegate()已被弃用。自jQuery 1.7以来它被.on()方法取代,所以它的使用已经不再使用了。

     

但是,对于早期版本,它仍然是使用事件委派的最有效方法。有关事件绑定和委派的更多信息,请参见 on() 方法。

使用事件委派 on() 代替:

$('body').on('click', '#comment-list .pagination a', function() {
    ....
});  

希望这有帮助。