JQuery类选择器只对原始类名元素起作用
JQuery类选择器只对原始类名元素起作用,对后添加的class不起作用。
html代码:
<body> <h3 align="center">joeking</h3> <hr> <p>p1</p><br> <p id="p">p2</p><br> <p>p3</p> <p name="code">$(document).ready(function(){$("p").click(function(){$("p").addClass("red");});$("#p").click(function(){$(this).removeClass("red");});$(".red").click(function(){alert("this is red");});});css:
.red{color:red}p{text-align:center}代码如上,只有单击p4标签时,才会弹出alert对话框。
使用火狐firebug查看html,所有的p元素都添加了class,但是为什么不起作用呢?