Home >Web Front-end >JS Tutorial >How to use javaScript event binding

How to use javaScript event binding

PHP中文网
PHP中文网Original
2017-06-20 10:03:291295browse

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:

  1. Bind directly in the DOM element;

  2. Bind in JavaScript code;

  3. 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.

##②.attachEvent(eventName,handle);

elementObjectDOM object (i.e. DOM element). eventNameEvent 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. handleEvent 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn