1

我有这段代码用于加载内容以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我在Version中测试我的代码,我认为delegatejQuery 2.1.1.

如何解决我的问题?

4

1 回答 1

2

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

然而,对于早期版本,它仍然是使用事件委托的最有效方法。有关事件绑定和委托的更多信息在on()方法中。

改用事件委托on()

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

希望这可以帮助。

于 2016-10-25T08:44:20.467 回答