我正在开发一个正在使用的应用程序
$("#plblAgreeProblem",".plblAgreeComment").live("click", function(){
alert("bil");
}
但类选择器不起作用。我的具有 class="plblAgreeComment" 的 div 正在动态创建,所以我正在使用 .live()
请帮我。
我正在开发一个正在使用的应用程序
$("#plblAgreeProblem",".plblAgreeComment").live("click", function(){
alert("bil");
}
但类选择器不起作用。我的具有 class="plblAgreeComment" 的 div 正在动态创建,所以我正在使用 .live()
请帮我。
问题不是很清楚。对于选择器,请尝试使用以下内容。
$("#plblAgreeProblem, .plblAgreeComment").live("click", function(){
alert("bil");
});
或者
$(".plblAgreeComment").live("click", function(){
alert("bil");
});
首先,您应该知道live
在当前的 jQuery 版本中已弃用它,您应该on
改用它。
也就是说,您的选择器$("#plblAgreeProblem",".plblAgreeComment")
将搜索#plblAgreeProblem
id
带有plblAgreeComment
class
. 是你要写的选择器吗?
另一个注意事项,id
必须是唯一的,这意味着页面中不能有多个具有相同id
属性的元素。
哦..你忘记了函数);
的末尾:live
$("#plblAgreeProblem",".plblAgreeComment").live("click", function(){
alert("bil");
});
如果您的“plblAgreeComment”类元素位于节点“#plblAgreeProblem”的内部,请尝试使用不同的选择器:
$("#plblAgreeProblem .plblAgreeComment")
如果您使用的是较新的 jQuery 版本,并且包含“.on”,请使用:
$(document).on("click", "#plblAgreeProblem, .plblAgreeComment" ,function(){
alert("bil");
});
注意:如果可能的话,请尝试绑定到比“文档”更近的东西 - 例如标记该部分的包装器等,以解决速度问题。
其他答案涵盖以前的版本
这是一个包装好的工作示例:http: //jsfiddle.net/MarkSchultheiss/pP6nL/