onload事件和window,document,body的研究,onloaddocument
今天在工作中用到了onload事件,發現了一些有趣的事情,比如一般來說,如果我們需要給一個DOM結構綁定一個事件,我們一般會採用如下方法(以Window對象為例):
【現象】
window.onload = function() {console.log('<span style="font-family: Arial, Helvetica, sans-serif;">window.onload</span><span style="font-family: Arial, Helvetica, sans-serif;">');</span>}; window.addEventListener && window.addEventListener("load", function() {console.log('window.addEvent');});執行這兩個方法,可以看到如下:
說明,這兩個方法都被觸發了,在這一點上IE表現也是一致的。
接下來我們來試試document對象
document.onload = function() {console.log('document.onload');}; window.addEventListener && document.addEventListener("load", function() {console.log('<span style="font-family:Arial, Helvetica, sans-serif;">document</span>.addEvent');});window.attachEvent && document.attachEvent('onload', function() {console.log('document.attachEvent');});執行後發現,document的onload事件無論是哪種方式都不會觸發。
再接下來,我們來試試body對象
document.body.onload = function() {console.log('body.onload');};window.addEventListener && document.body.addEventListener("load", function() {console.log('body.addEvent');});window.attachEvent && document.body.attachEvent('onload', function() {console.log('body.attachEvent');});結果如:
可以看到,說明通過這種寫法只有onload方式綁定的被觸發了。
還有更好玩的,當同時給body和window使用onload方法時,只有window的onload方法觸發
具體代碼如下:
document.body.onload = function() {console.log('body.onload');};window.onload = function() {console.log('window.onload');}執行結果如:
【結論】
所以總結如下:
1、onload和addEventListener當使用在window上的時候都觸發
2、當使用在document時,都不觸發
3、當使用在body上時,只有onload觸發
4、當body和window同時使用onload方法時,body的方法會被吃掉。
【深入研究】
這裡的研究主要是參看了w3c草案中的一些說明,怕翻譯不好,貼出原文,貌似load事件和其他的有很多不同,
6.1.6.3 Event firing
Certain operations and methods are defined as firing events on elements. For example, the click() method on the HTMLElement interface is defined as firing a click event on the element. [DOMEVENTS]
Firing a simple event named e means that an event with the name e, which does not bubble (except where otherwise stated) and is not cancelable (except where otherwise stated), and which uses the Event interface, must be dispatched at the given target.
Firing a synthetic mouse event named e means that an event with the name e, which does not bubble (except where otherwise stated) and is not cancelable (except where otherwise stated), and which uses the MouseEvent interface, must be dispatched at the given target. The event object must have its screenX, screenY, clientX, clientY, and button attributes set to 0, its ctrlKey, shiftKey, altKey, and metaKey attributes set according to the current state of the key input device, if any (false for any keys that are not available), its detailattribute set to 1, and its relatedTarget attribute set to null. The getModifierState() method on the object must return values appropriately describing the state of the key input device at the time the event is created.
Firing a click event means firing a synthetic mouse event named click, which bubbles and is cancelable.
The default action of these events is to do nothing except where otherwise stated.
6.1.6.4 Events and the
Window object
When an event is dispatched at a DOM node in a Document in a browsing context, if the event is not a load event, the user agent must also dispatch the event to the Window, as follows:
另外就是,在第6.1.6.2節中有詳細描述window,document,body對象對各事件的支援,因為太長,就不貼出來了。
Ps:本文純屬個人研究,如有謬誤,煩請指出,萬分感謝。
javascript中windowonload與body onload怎解決
onload事件是window對象才有的,<body onload="func">這樣設定只是為了方便,其實仍然是window.onload=func,且window.onload只允許指定一次,衝突是必然的;如果要指定多個,那隻能把多個函數放進一個函數裡調用。
js body onload事件的處理
window.onload = function(){ alert('window load');}document.body.onload = function(){ alert('body load');}