$('body')。on('click','.anything',function(){}) [英] $('body').on('click', '.anything', function(){})
问题描述
// code
});(pre $)
目前无法处理任何事情,我无法弄清楚原因。我能够锚定其他任何东西,比如说我只是在 body
内部抛出一个 #wrap
div。然后,我可以做$ / $>
$('#wrap')。on('click','.anything',函数(){
//代码
});
代表我想要的任何元素。
任何想法我可以做什么来禁用身体元素的这种能力?
谢谢!
您应该使用 $(文件)
。它是文档中任何点击事件的函数触发器。然后在里面你可以使用jquery on(click,body *,somefunction)
,其中第二个参数指定了哪个特定元素作为目标。在这种情况下,身体内的每个元素。
$ p $
$(document).on('click','body *',function(){
// $(this)=您当前点击的元素。
//附加代码
});
$('body').on('click', '.anything', function() {
//code
});
doesn't work for anything right now and I can't figure out why. I'm able to anchor to anything else, say I just toss a #wrap
div right inside the body
. Then I'm able to do
$('#wrap').on('click', '.anything', function() {
//code
});
for any element I want.
Any idea what I could have done to disable this ability on the body element?
Thanks!
You should use $(document)
. It is a function trigger for any click event in the document. Then inside you can use the jquery on("click","body *",somefunction)
, where the second argument specifies which specific element to target. In this case every element inside the body.
$(document).on('click','body *',function(){
// $(this) = your current element that clicked.
// additional code
});
这篇关于$('body')。on('click','.anything',function(){})的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!