【发布时间】:2016-09-12 14:18:02
【问题描述】:
当我单击具有类 oldClass 的单词 Hello 时,jquery 将类更改为类 newClass,它将单词 Hello 蓝色着色。这意味着班级发生了变化。为什么jquery没有响应点击类newClass?
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).ready(function(){
$(".oldClass").click(function(){
$(".oldClass").attr("class","newClass");
});
$(".newClass").click(function(){
$(".newClass").attr("class","oldClass");
});
});
</script>
<style>
.oldClass {color:red;}
.newClass {color:blue;}
</style>
</head>
<body>
<p class="oldClass">Hello</p>
</body>
【问题讨论】:
-
使用
.toggleClass()Demo -
仅供参考:jQuery 实现 addClass 和 removeClass 函数api.jquery.com/addclass
标签: jquery