myClass 和 noClass 样式名在所有段落上被移除, 然后 yourClass 被添加。
自 jQuery 1.4开始, .addClass() 方法允许我们通过传递一个用来设置样式类名的函数。
$("ul li:last").addClass(function(index) {
return "item-" + index;
});
给定一个有2个<li>元素的无序列表,这个例子将在最后一个<li>元素上加上"item-1"样式。
例子:
Example: 在匹配的元素上加上'selected'样式。
<!DOCTYPE html>
<html>
<head>
<style>
p { margin: 8px; font-size:16px; }
.selected { color:blue; }
.highlight { background:yellow; }
</style>
<script src="https://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>Hello</p>
<p>and</p>
<p>Goodbye</p>
<script>
$("p").last().addClass("selected");
</script>
</body>
</html>
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64065.shtml