开发者

jQuery - Why .att doesn't replace the attribute value?

开发者 https://www.devze.com 2023-01-13 20:13 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消