开发者

How come mousdown doesn't work on <html>-tag?

The mousedown-event only works if I click an elemnt, not the html-element. Why and What can I do about it?

var curFocus;
$(document.body).delegate('*','mousedown', function(){
    if ((thi开发者_StackOverflow中文版s != 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
});


The <html> tag is a bit to general, it includes non-visible elements like the <head>, try using the <body> tag.


The <body> element is inside the <html> element, not the other way around. And your code will only respond to mouse-down events coming from children of the body element, not even the <body> element itself. See http://jsfiddle.net/5T8tW/ for a demo.

To make the code respond to such events, change $(document.body) to $(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. Note that as the event bubbles up through the DOM tree, your event handler may be fired more than once; use return false; at the end to prevent that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜