Another way to bind an event is to use AddEventListener () or attachevent () to bind the event listener function.
AddEventListener () function syntax:
Elementobject.addeventlistener (eventname,handle,usecapture);
Parameters |
Description |
Elementobject |
A DOM object (that is, a DOM element). |
EventName |
The event name. Note that the event name here does not have "on", Like mouse click events click, mouse double click event DoubleClick, The mouse moves into the event mouseover, the mouse moves out the event mouseout and so on. |
Handle |
The event handle function, which is the function used to handle the event. |
Usecapture |
Boolean type, whether capture is used, general use false 。 This involves the concept of JavaScript event flow, which will be explained in detail in subsequent chapters. |
Attachevent () function syntax:
Elementobject.attachevent (Eventname,handle);
Parameters |
Description |
Elementobject |
A DOM object (that is, a DOM element). |
EventName |
The event name. Note that, unlike AddEventListener (), The event name here has "on", such as mouse click event onclick, Mouse double-click event OnDoubleClick, mouse move into event onmouseover, Mouse move out event onmouseout, etc. |
Handle |
The event handle function, which is the function used to handle the event. |
Note: The event handle function refers to "function name" and cannot be enclosed with parentheses.
AddEventListener () is the standard method of binding event listener functions, supported by the consortium, Chrome, FireFox, Opera, Safari, This function is supported by IE9.0 and above, but IE8.0 and the following versions do not support this method, and it uses attachevent () to bind the event listener function. Therefore, this method of binding an event must handle browser compatibility issues.
JS native future element listening notation