最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does HTML 'body' tag have an onblur event? - Stack Overflow

programmeradmin4浏览0评论

I tried to bind the onblur event to a <body> tag and it works in IE 6/8.

However, when I turn to w3schools, it turns out that the <body> tag doesn't have an onblur event.

Can anyone explain this?

I tried to bind the onblur event to a <body> tag and it works in IE 6/8.

However, when I turn to w3schools, it turns out that the <body> tag doesn't have an onblur event.

Can anyone explain this?

Share Improve this question edited Oct 1, 2013 at 6:15 BBdev 4,9382 gold badges33 silver badges45 bronze badges asked Aug 4, 2011 at 9:30 ForedoomedForedoomed 2,2392 gold badges21 silver badges39 bronze badges 1
  • probably w3fools. knows the answer. – jAndy Commented Aug 4, 2011 at 9:31
Add a ment  | 

3 Answers 3

Reset to default 3

The HTML4 specification does not list the onblur attribute.

The HTML5 draft on the other hand does.

body:onblur is part of the HTML5 spec, but not part of the HTML4 spec.

If you want to support HTML4 browsers, why not use window:onblur? For most cases, you'll have the same functionality.

I would never rely on the source you mentioned. The problem is, MSIE fires the blur event for almost every element, <body included. Wheres most other implementations do the exact oposite.

It might help if you explicitly the a tabIndex for the body element, for instance:

document.body.tabIndex = 1;
document.body.onblur = function() {
    alert('body blur');
};

That works for me on FF5. That said, I'm not sure how many versions from FF and how much implementations support that 'tricky' workaround.

The next logical question is, why would you want to fire the body a blur event ? The only reasonable thing I can think of, is that you want to know when someone leaves your page ? If that is the case, I would remend to watch the window itself instead of the body.

window.onblur = function() {
    alert('window blur');
}
发布评论

评论列表(0)

  1. 暂无评论