jQuery添加和删除类 [英] jQuery Add and Remove Class
本文介绍了jQuery添加和删除类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个选择器:
< select name =typeid =type>
< option class =sub_1value =1> 1< / option>
< option class =sub_2value =2> 2< / option>
< option class =sub_3value =3> 3< / option>
< option class =sub_3value =4> 4< / option>
< / select>
然后我有这个:
< select name =amountid =amount>
< option class =id =valueAdd>
< / select>
所以,我想知道我如何只能添加一个类。我有下面的代码,虽然,如果我的例子在我的#type选择一个不同的OPTION,然后它保留第一个类,然后只是添加第二个类。
<我怎么能这样做只添加一个类? (即替换另一个)
jQuery:
$文档).ready(function(){
$(#type)。change(function(){
var typeid =($(this).val())
$(#valueAdd)。addClass(sub_+
typeid);
});
});
解决方案
先执行removeClass,然后addClass
$(#valueAdd)removeClass()。addClass(sub_+ typeid);
http://api.jquery.com/removeClass/
I have this selector:
<select name="type" id="type">
<option class="sub_1" value="1">1</option>
<option class="sub_2" value="2">2</option>
<option class="sub_3" value="3">3</option>
<option class="sub_3" value="4">4</option>
</select>
And then I have this one:
<select name="amount" id="amount">
<option class="" id="valueAdd">
</select>
So, I wish to know how I can only add ONE class to that option. I have this code below, although, if I example select a different OPTION in my #type select, then it keeps the first class, and then just adds the second class too.
How can I do so it only adds one class? (Ie replace the other one)
jQuery:
$(document).ready(function() {
$("#type").change(function() {
var typeid = ($(this).val())
$("#valueAdd").addClass("sub_" +
typeid);
});
});
解决方案
Do removeClass first then addClass
$("#valueAdd").removeClass().addClass("sub_" + typeid);
http://api.jquery.com/removeClass/
这篇关于jQuery添加和删除类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文