jq类名选择器对使用jq添加类名的元素无效,也就是说只对原始类名有用……比如我用jq给一个元素加一个类名……之后我想对这个元素进行操作,可是就无法使用类名选择器来操作这个元素了……
这是因为你新添加的类在一开始并不存在,你直接用$().click()就会没有响应,你可以用delegate()方法绑定事件,具体你可以查一下这个函数的使用方法。我这里给个例子,你看看有没有启发
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
>
<
title
>TEST</
title
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=utf-8"
/>
<
script
src
=
"http://code.jquery.com/jquery-latest.js"
></
script
>
<
script
>
$(function () {
var $comment = $("#comment");
$(".bigger").click(function () {
$(".small").addClass('smaller');
$comment.height($comment.height() + 50);
});
$(".msg").delegate('.smaller', 'click', function () {
$comment.height($comment.height() - 50);
});
// 如果用下面的,就没有效果
// $(".smaller").click(function () {
// $comment.height($comment.height() - 50);
// });
});
</
script
>
</
head
>
<
body
>
<
form
action= "#">
<
div
class
=
"msg"
>
<
div
class
=
"caption"
>
<
span
class
=
"bigger"
>bigger</
span
>
<
span
class
=
"small"
>smaller</
span
>
</
div
>
<
div
class ="content">
<
textarea
id
=
"comment"
rows = "8"
cols
=
"20"
>this is a Jquery test!</
textarea
>
</
div
>
</
div
>
</
form
>
</
body
>
</
html
>