jQuery和两种情况之间的切换 [英] jQuery and switch between two cases
问题描述
我有这个代码
<a href="#">
<span style="border-right: medium none;" class="Yes">Yes</span>
</a>
<a href="#">
<span class="No">No</span>
</a>
如果否是current
,我想在单击是"类时调用"current
",并在是"范围内添加,然后从否"中删除current
类.
if no is current
I want when click on yes class calling current
adding on yes span and delete current
class from no.
如果是current
,我想在没有类的情况下单击,调用current
,不加跨度,并从是中删除current
类.
If yes current
I want when click on no class calling current
adding on no span and delete current
class from yes.
我希望每种情况都可以一键发生.
I want each case happen in just one click.
推荐答案
这里是一个示例.这是第一个切入点,可能是可以优化的.我要做的就是从所有元素中删除current
类,并将其添加到当前元素中.
Here is a sample. This is a first cut, may be this can be optimized. All I am trying to do is remove current
class from all the elements and add it to current one.
您必须修改html以使其具有唯一的类
You have to modify your html to have a unique class
<a href="#">
<span style="border-right: medium none;" class="Yes yesno">Yes</span>
</a>
<a href="#">
<span class="No yesno">No</span>
</a>
这是jQuery代码
$('.yesno').click(function(){
$('.yesno').each(function(i, v){
$(v).removeClass('current');
});
$(this).addClass('current');
});
看看是否有帮助.
这篇关于jQuery和两种情况之间的切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!