jQuery - Why .att doesn't replace the attribute value?
Based on the manual as follows:
http://api.jquery.com/attr/
attr( attributeName, value )
I should be able to replace the attribute value of a selected element.
<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" />
开发者_StackOverflow </div>
</div>
I need to replace the class value of label for keynote1 from labeloptional to label.
Here is what have done:
$("label[for=keynote1]").att('class', 'label');
However, the class value for label of keynote1 still is 'labeloptional' after the execution of the above statement.
why?
Thank you
You need to use attr
not att
.
However, if you're changing classes, you shouldn't be using attr()
.
What you should do is use removeClass()
and addClass()
.
$('label').removeClass('oldClass').addClass('newClass');
That way, you won't be overwriting any other classes that might be assigned to a tag.
精彩评论