代码改变世界

.addClass()和removeClass()

2011-12-15 21:02  youxin  阅读(229)  评论(0编辑  收藏  举报
<!DOCTYPE html>
<html>
<head>
<style>
div
{ background: white; }
.red
{ background: red; }
.red.green
{ background: green; }
</style>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>

<div>This div should be white</div>
<div class="red">This div will be green because it now has the "green" and "red" classes.
It would be red if the addClass function failed.</div>
<div>This div should be white</div>
<p>There are zero green divs</p>

<script>
$(
"div").addClass(function(index, currentClass) {
var addedClass;

if ( currentClass === "red" ) {
addedClass
= "green";
$(
"p").text("There "); //改变文本
}

return addedClass;
});
</script>

</body>
</html>