dongluan6784 2011-12-29 00:47
浏览 69
已采纳

jQuery,foreach div in ul

How I can do a foreach or similar in jQuery? I need that when the user clicks in the <span>, add the class inactive to all <div>s with the vote active class in the <ul>.

I've tried with the .each method, but it doesn't work.

I have this HTML:

<ul class="suggestions inactive">
    <li id="s2">
        <div class="id">2</div>
        <div class="vote inactive">
            <span class="up"></span>
            <span class="down"></span>
        </div>
        <div class="text">test2</div>
        <div class="rating">2</div>
    </li>
    <li id="s3">
        <div class="id">3</div>
        <div class="vote active">
            <span class="up"></span>
            <span class="down"></span>
        </div>
        <div class="text">test3</div>
        <div class="rating">0</div>
    </li>
</ul>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="script.js"></script>

And this script:

            $(document).ready(function(){

                var ul = $('ul.suggestions');

                // Listening of a click on a UP or DOWN arrow:

                $('div.vote span').live('click',function(){

                    var elem        = $(this),
                        parent      = elem.parent(),
                        li          = elem.closest('li'),
                        ratingDiv   = li.find('.rating'),
                        id          = li.attr('id').replace('s',''),
                        v           = 1,
                        ul2         = elem.closest('li').parent();

                    // If the user's already voted:
                    if(parent.hasClass('inactive')){
                        return false;
                    }
                    if(ul2.hasClass('inactive')){           
                        return false;
                    }

                    //If the user's already voted, add following class
                    parent.removeClass('active').addClass('inactive');                                  
                    ul2.removeClass('active').addClass('inactive');


                    if(elem.hasClass('down')){
                        v = -1;
                    }

                    // Incrementing the counter on the right:
                    ratingDiv.text(v + +ratingDiv.text());

                    // Turning all the LI elements into an array
                    // and sorting it on the number of votes:

                    var arr = $.makeArray(ul.find('li')).sort(function(l,r){
                        return +$('.rating',r).text() - +$('.rating',l).text();
                    });

                    // Adding the sorted LIs to the UL
                    ul.html(arr);

                    // Sending an AJAX request
                    $.get('ajax.php',{action:'vote',vote:v,'id':id});
                });
  • 写回答

3条回答 默认 最新

  • donglei1973 2011-12-29 00:54
    关注

    You don't need .each() here at all.

    Why? Because jQuery's selectors wonderfully select everything of a single selector type. $('p') selects every p in your HTML.

    You should use

    $(this).parent().addClass('active').removeClass('inactive');`
    

    from within the scope of the

    $('div.vote span').live('click',function() { ... } );
    

    Also, remember that .live() is deprecated in jQuery 1.7+.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序