我一个开发一个应用程序中,我使用jQuery的类选择
$("#plblAgreeProblem",".plblAgreeComment").live("click", function(){
alert("bil");
}
但类选择不工作。我的div class =“plblAgreeComment”正在动态创建,所以我使用.live()
请帮助我。
我一个开发一个应用程序中,我使用jQuery的类选择
$("#plblAgreeProblem",".plblAgreeComment").live("click", function(){
alert("bil");
}
但类选择不工作。我的div class =“plblAgreeComment”正在动态创建,所以我使用.live()
请帮助我。
如果你正在使用jQuery版本较新,且包括了 “对” 使用:
$(document).on("click", "#plblAgreeProblem, .plblAgreeComment" ,function(){
alert("bil");
});
注意:如果可能的话,请尝试绑定比“文档”更接近的东西 - 例如标记的该部分的包装等,以便处理速度问题。
其他答案覆盖之前的版本
这里是它的工作示例包:http://jsfiddle.net/MarkSchultheiss/pP6nL/
注意:我根据你的另一个问题做了一个疯狂的假设,那就是两个截然不同的选择器,而不是另一个。是不正确的,如果您发布标记,我们会改进。 – 2012-04-16 19:29:22
问题不是很清楚。对于选择器,请尝试使用以下内容。
$("#plblAgreeProblem, .plblAgreeComment").live("click", function(){
alert("bil");
});
或
$(".plblAgreeComment").live("click", function(){
alert("bil");
});
首先,你应该知道,live
在当前的jQuery版本过时,你应该使用on
代替。
这就是说,你的选择$("#plblAgreeProblem",".plblAgreeComment")
,将搜索元素与与plblAgreeComment
class
的#plblAgreeProblem
id
的元素。它是你想写的选择器吗?
另一个说明,id
必须是唯一的,这意味着页面中不能多于一个元素具有相同的id
属性。
喔..你在live
函数结束时忘了);
:
$("#plblAgreeProblem",".plblAgreeComment").live("click", function(){
alert("bil");
});
如果与类“plblAgreeComment”你的元素在里面的节点“#plblAgreeProblem”尝试使用不同势选择而不是:
$("#plblAgreeProblem .plblAgreeComment")
什么不起作用? – Bergi 2012-04-16 19:21:43
你能提供你的HTML吗? – 2012-04-16 19:22:42
警报不叫 – Billz 2012-04-16 19:22:54