如何覆盖子元素的 jQuery 绑定?
我的鼠标操作触发了 html 和 body 上的自定义函数,但是页面上的滚动条和表单现在对鼠标单击没有响应!
$('html,body').bind('mousedown mouseup mouseover mousemove', function(e){
e.preventDefault(); //this is required :(
customFunction();
});
(为什么我需要防止默认?完整的自定义函数此处)
如何在保持表单可用的同时保留正文绑定滚动条起作用了吗?
我这里有一个 JSFiddle 演示,准确地展示了正在发生的事情:
http://jsfiddle.net/Ywg9T/
I have mouse actions triggering a custom function on the html and body, however the scroll bars and a form I have on the page has now become unresponsive to mouse clicks!
$('html,body').bind('mousedown mouseup mouseover mousemove', function(e){
e.preventDefault(); //this is required :(
customFunction();
});
(Why do I need prevent default? full customFunction here)
How do I preserve the body binding while keeping the form usable and the scroll bars functioning?
I have a JSFiddle demo here, showing exactly what is happening:
http://jsfiddle.net/Ywg9T/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 jQuery 中的一个错误,与开始选择的事件相关。实在是太频繁了。已为此提交了一张票,并计划在 1.8 中修复。同时,您可以通过黑客方式删除该事件处理程序来解决此问题(其中“element”是您想要禁用它的元素 - 在您的情况下,是整个文档):
然后您不必执行
阻止默认
。所以,你的代码看起来像这样:This is a bug in jQuery, related to the event for starting a selection. It fires way too frequently. There is a ticket filed for it, and it scheduled to be fixed in 1.8. In the meantime, you can fix this problem by hackily removing that event handler (where 'element' is the element you want to disable it for -- in your case, the whole document):
Then you don't have to do
preventDefault
. So, your code would look something like this: