代码之家  ›  专栏  ›  技术社区  ›  CLiown

使用Ajax时jQuery出现了奇怪的问题

  •  2
  • CLiown  · 技术社区  · 14 年前

    我使用jquery/php/mysql在页面上加载twitter搜索结果,但仅限于前20个搜索结果。

    当用户滚动页面并点击页面底部时,将加载并显示另外20个结果。

    我已经实施了投票系统,所以这些特定的推特可以上下投票。

    但是,当加载滚动结果时,此功能将停止对Ajax加载结果的工作,但继续对前20个结果进行工作。

    以下是获取滚动新结果的jquery:

    j(window).scroll(function(){
        if (j(window).scrollTop() == j(document).height() - j(window).height()){
            j.ajax({
                url: "older.php?oldest="+oldestName+"",
                cache: false,
                success: function(html){
                    j(".older").html(html);
                    j('.tweets ul li').removeClass( 'last_item' );
                    j('.older ul > *').clone().hide().appendTo('.tweets ul').slideDown();
                }
            })
        }
    }); 
    

    以及设置投票的jquery:

            $("a.vote_up").click(function(){
                //get the id
                the_id = $(this).attr('id');
    
                // show the spinner
                $(this).parent().html("<img src='images/spinner.gif'/>");
    
                //fadeout the vote-count 
                $("span#votes_count"+the_id).fadeOut("fast");
    
                //the main ajax request
                $.ajax({
                type: "POST",
                data: "action=vote_up&id="+$(this).attr("id"),
                url: "vote.php",
                    success: function(msg) {
                        $("span#votes_count"+the_id).html(msg);
                        //fadein the vote count
                        $("span#votes_count"+the_id).fadeIn();
                        //remove the spinner
                        $("span#vote_buttons"+the_id).remove();
                    }
                });
            });
    

    HTML标记:

    <li id='1283009589'>
    <a class='imglink' target='_blank' href='link'>
    <img src='link'alt='howtomoodle' title='howtomoodle' />
    </a>
    <a class='userlink' target='_blank' href='http://twitter.com/jim' alt='jim' title='jim'>jim</a>
    
    Screenpresso - free screen capture software http://post.ly/uFxt  #moodle || 28-08-2010 16:33
    
    <span class='votes_count' id='votes_count22361731118'>Votes:0</span>
    <span class='vote_buttons' id='vote_buttons22361731118'>
    <a href='javascript:;' class='vote_up' id='22361731118'></a>
    <a href='javascript:;' class='vote_down' id='22361731118'></a>
    </span>
    
    </li>
    

    有人知道为什么要加载旧的结果吗?他们不允许单击voteup和votedown按钮,或者至少不允许单击.click事件。

    2 回复  |  直到 14 年前
        1
  •  1
  •   user113716    14 年前

    替代使用的好方法 .live() .delegate() ,当放置在动态元素的父容器上时效率更高。

    $('#theContainer').delegate("a.vote_up", "click", function() {
            //get the id
            the_id = $(this).attr('id');
    
            // show the spinner
            $(this).parent().html("<img src='images/spinner.gif'/>");
    
            //fadeout the vote-count 
            $("span#votes_count"+the_id).fadeOut("fast");
    
            //the main ajax request
            $.ajax({
            type: "POST",
            data: "action=vote_up&id="+$(this).attr("id"),
            url: "vote.php",
                success: function(msg) {
                    $("span#votes_count"+the_id).html(msg);
                    //fadein the vote count
                    $("span#votes_count"+the_id).fadeIn();
                    //remove the spinner
                    $("span#vote_buttons"+the_id).remove();
                }
            });
        }
    );
    

    (需要jquery 1.4或更高版本。)

        2
  •  2
  •   migajek    14 年前

    这是因为“a.vote”的“click”事件在站点加载时绑定一次。当您加载新内容(接下来的20个结果)时,不会调用它们$(document).ready。看看$.Live方法 http://api.jquery.com/live/ 这正是你需要的:)