基于以下手册:
http://api.jquery.com/attr/
attr( attributeName, value )
我应该能够替换所选元素的属性值。
<div id="keynote1Fld" class="row ">
<label for="keynote1" class="labeloptional">Topic 1</label>
<div class="collection">
<input type="text" class="largetextfield" maxlength="96" size="96" value="" id="keynote1" name="keynote1" />
</div>
</div>
我需要将label的类值从labeloptional替换为keynote1。
下面是我们所做的工作:
$("label[for=keynote1]").att('class', 'label');
但是,在执行上述语句之后,keynote1的label的类值仍然是'labeloptional‘。
为什么?
谢谢
发布于 2010-09-01 21:58:47
您需要使用attr
,而不是att
。
但是,如果您要更改类,则不应该使用attr()
。
你应该做的是使用removeClass()
和addClass()
。
$('label').removeClass('oldClass').addClass('newClass');
这样,您就不会覆盖可能分配给标记的任何其他类。
https://stackoverflow.com/questions/3618812
复制相似问题