[英]How come mousdown doesn't work on <html>-tag?
The mousedown-event only works if I click an elemnt, not the html-element. 仅当我单击一个元素而不是html元素时,mousedown事件才起作用。 Why and What can I do about it?
为什么以及我该怎么办?
var curFocus;
$(document.body).delegate('*','mousedown', function(){
if ((this != curFocus) && // don't bother if this was the previous active element
($(curFocus).is('.field')) // if it was a .field that was blurred
) {
$('.' + $(curFocus).attr("id")).removeClass("visible"); // take action based on the blurred element
}
curFocus = this; // log the newly focussed element for the next event
});
<html>
标记有点笼统,它包含不可见的元素,例如<head>
,请尝试使用<body>
标记。
The <body>
element is inside the <html>
element, not the other way around. <body>
元素位于<html>
元素内部,而不是相反。 And your code will only respond to mouse-down events coming from children of the body element, not even the <body>
element itself. 而且您的代码将仅响应来自body元素子元素的鼠标按下事件,甚至不响应
<body>
元素本身。 See http://jsfiddle.net/5T8tW/ for a demo. 有关演示,请参见http://jsfiddle.net/5T8tW/ 。
To make the code respond to such events, change $(document.body)
to $(document)
. 要使代码响应此类事件,请将
$(document.body)
更改为$(document)
。 If you don't want to respond to events coming from the <html>
element but only its children, you could use $(document.documentElement)
, which is the <html>
element. 如果您不想响应来自
<html>
元素的事件,而仅响应其子元素,则可以使用$(document.documentElement)
,它是<html>
元素。 Note that as the event bubbles up through the DOM tree, your event handler may be fired more than once; 请注意,随着事件在DOM树中冒泡,您的事件处理程序可能会被触发多次; use
return false;
使用
return false;
at the end to prevent that. 最后防止这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.