Jquery: hasClass addClass removeClass toggleClass 用法
<1>hasClass 应用
Examples:
HTML
<div id="myElement" class="testClass"></div>
JavaScript
$('myElement').hasClass('testClass'); // returns true
<2>addClass 应用
Examples:
HTML
<div id="myElement" class="testClass"></div>
JavaScript
$('myElement').addClass('newClass');
<3>removeClass 应用
Examples:
HTML
<div id="myElement" class="testClass newClass"></div>
JavaScript
$('myElement').removeClass('newClass');
Resulting HTML
<div id="myElement" class="testClass"></div>
<4>toggleClass 应用
Adds or removes the passed in class name to the Element, depending on whether or not it's already present.
Examples:
HTML
<div id="myElement" class="myClass"></div>
JavaScript
$('myElement').toggleClass('myClass');
Resulting HTML
<div id="myElement" class=""></div>
JavaScript
$('myElement').toggleClass('myClass');
Resulting HTML
<div id="myElement" class="myClass"></div>
转自:http://www.blogjava.net/fengzhanke/archive/2011/01/11/342798.html