Event binding is to operate on something. (Because you want it to achieve any effect, you have to bind it, hahaha!)
If you want JavaScript to respond to user operations, you must first bind an event handler to the DOM element. The so-called event processing function is a function that processes user operations. Different operations correspond to different names.
In JavaScript, there are three commonly used methods of binding events:
Bind directly in the DOM element;
Bind in JavaScript code;
Bind event listening function.
1. Let me explain to you how to bind directly in DOM elements in the form of examples.
Click a button to pop up a warning box
Isn’t it very simple, then continue...
2. Binding in JavaScript code (binding events can separate JavaScript code from HTML tags, and the document structure is clear and easy to manage) and development)
3. Bind event listening. Another way to bind events is to use addEventListener() or attachEvent() to bind the event listening function.
①.addEventListener(eventName,handle,useCapture).
Standard method of binding event listening function
eventName | Event name. Note that the event names here do not have "on", such as mouse click event click, mouse double-click event doubleclick, mouse move-in event mouseover, mouse move-out event mouseout, etc. |
handle | Event handle function is a function used to handle events. |
useCapture |
Boolean type, whether to use capture, generally use false.
|
DOM object (i.e. DOM element). | |
Event name. Note that unlike addEventListener(), the event names here have "on", such as mouse click event onclick, mouse double click event ondoubleclick, mouse move in event onmouseover, mouse move out event onmouseout, etc. | |
Event handle function is a function used to handle events. |
The above is the detailed content of How to use javaScript event binding. For more information, please follow other related articles on the PHP Chinese website!