撤消点击(function() {return false;})
我有一个疑问/问题。在某些时候,我需要除一个 div 之外的每个元素都是不可点击的,并且我有:
$('#MainDiv *:not(#Div)').click(function() {return false;});
在 MainDiv 中,我有几个其他元素(a、img...),并且我需要这些元素重新调整为可点击。可以撤消该功能,还是我需要再次设置这些元素的每个 a.href 和 img.click ?
I have a question/problem. At some point I need that every element are non-clickable except one div and I have:
$('#MainDiv *:not(#Div)').click(function() {return false;});
In the MainDiv i have several other elements (a, img,...) and I need those elements retun to be clickable. It is possible to undo that function or do I need to set every a.href and img.click of those elements again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试使用
$('#MainDiv *:not(#Div)').unbind("click");
然后,当您需要重新分配单击函数时,只需确保每次单击的作用都包含在其 on 函数中,这样您就可以简单地执行类似
$(".someEle").click(function(e){ yourCustomeFuncForThisItem (var1, var2) });
Try using
$('#MainDiv *:not(#Div)').unbind("click");
Then when you need to reasign the the click functions, just make sure what each click does is wrapped in its on function so you can simply do something like
$(".someEle").click(function(e){ yourCustomeFuncForThisItem(var1, var2); });
这是事件委托的工作。 #MainDiv 可能有几十个或几百个元素?只需在此处附加一键单击事件处理程序,让事件冒泡到#MainDiv,然后查看单击的内容是否为#Div。
This is a job for event delegation. #MainDiv probably has dozens or hundreds of elements? Just attach one click event handler there, let the event bubble to #MainDiv and see if the thing that was clicked is #Div.
您可以尝试以下操作:
$('#MainDiv *:not(#Div)').bind('click', false);
应该相当于$('#MainDiv *: not(#Div)').click(function() { return false; });
You can try this:
$('#MainDiv *:not(#Div)').bind('click', false);
should be equivalent to$('#MainDiv *:not(#Div)').click(function() { return false; });