我正在尝试按类和 id 选择向下投票箭头。所以看来我可以选择第一次尝试: $("span#"+obj_id+".voteup").removeClass('voteup').addClass('votedup');
但我无法选择第二次尝试: $("span#"+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;">
<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>