jquery 1.7.1不能通过类和id的交集选择span

时间:2013-03-23 03:05:34

标签: jquery

我正在尝试通过class和id选择downvote箭头。所以我似乎可以选择第一次尝试:                         $( “跨度#” + obj_id + “voteup。”)removeClass( 'voteup')addClass( 'votedup');

但我无法选择第二次尝试:                         $( “#跨度” + obj_id + “votedown”)。removeClass( 'votedown')addClass( 'votedown2')。;

代码粘贴在下面:

Jquery

    $(function(){
        $("body").on("click","span.voteup", 
            function(){
                obj_id = $(this).attr('id');
                var theint = parseInt($("span#votesum"+obj_id).html());
                theint++;     
                $("a#vu"+obj_id).html('☺');
                $("a#vd"+obj_id).html('⬇');
                $("span#"+obj_id+".voteup").removeClass('voteup').addClass('votedup');
                $("span#"+obj_id+".votedown").removeClass('votedown').addClass('votedown2');   
                $("span#votesum"+obj_id).html(theint);                                               
                $.post(
                    '/vote/',
                    {'obj_id': obj_id, 'vote': 1}
                );              
            }
        );
    });

HTML

                            <span class="no_decoration">
                                {% if object.voted == 1 %}
                                    <span class="votedup" id= "{{object.obj_id}}">
                                        <a href='javascript:;' class='voteup' id="vu{{object.obj_id}}">☺</a>
                                    </span>
                                {% elif object.voted == -1 %}
                                    <span class="voteup2" id= "{{object.obj_id}}">
                                        <a href='javascript:;' class='voteup' id="vu{{object.obj_id}}">⬆</a>
                                    </span>
                                {% else %}
                                    <span class="voteup" id= "{{object.obj_id}}">
                                        <a href='javascript:;' class='voteup' id="vu{{object.obj_id}}">⬆</a>
                                    </span>
                                {% endif %}
                            </span>
                            <br>
                            <span style="color:black;">
                                &nbsp;<span id="votesum{{object.obj_id}}">{{object.votesum}}</span>
                            </span>
                            <br>
                            <span class="no_decoration">
                                {% if object.voted == -1 %}
                                    <span class="voteddown" id= "{{object.obj_id}}">
                                        <a href='javascript:;' class='votedown' id="vd{{object.obj_id}}">☹</a>
                                    </span>
                                {% elif object.voted == 1 %}
                                    <span class="votedown2" id= "{{object.obj_id}}">
                                        <a href='javascript:;' class='votedown' id="vd{{object.obj_id}}">⬇</a>
                                    </span>
                                {% else %}
                                  <span class="votedown" id= "{{object.obj_id}}">
                                    <a href='javascript:;' class='votedown' id="vd{{object.obj_id}}">⬇</a>
                                  </span>   
                                {% endif %} 
                            </span>
                        </span>

1 个答案:

答案 0 :(得分:1)

ID只能在页面上使用一次。 jQuery可能总是找到ID的第一个实例,这就是为什么第二次尝试(添加votedown类)没有找到任何内容。