" statement to bind; 2. Use the "dom object.on event name = event handler" statement to bind; 3. Use the "event source.addEventListener(event name, event handler, whether to capture)" statement to bind."> What are the ways to bind events in javascript-Front-end Q&A-php.cn

What are the ways to bind events in javascript

青灯夜游
Release: 2022-02-16 13:52:08
Original
5697 people have browsed it

How to bind events: 1. Use the "" statement to bind; 2. Use "dom object.on event name = event handling Program" statement binding; 3. Use the "event source.addEventListener(event name, event handler, whether to capture)" statement binding.

What are the ways to bind events in javascript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

In Javascript, there are three ways to bind events:

  • Use the event attribute binding of the HTML tag to bind the handler

  • Use the event attribute of the event source to bind the handler

  • Use addEventListener() to bind the handler

1. Use the event attribute binding handler of HTML tags

Basic syntax:

<标签 on事件名="事件处理程序" />
Copy after login

Example:

Copy after login

Sample code:

What are the ways to bind events in javascript

The above code is the most typical inline binding. Although it can complete the functions we need, it binds the structural style behavior in the same tag, which is not conducive to later maintenance.

2. Use the event attribute binding handler of the event source

Basic syntax:

dom对象.on事件名 = 事件处理程序(通常是一个匿名函数)
Copy after login

Through dynamic binding This idea improves the above question, and the effect is as shown in the following figure:

What are the ways to bind events in javascript

3. Use addEventListener() to bind the handler

Although the method of binding event handlers using the event attribute of the event source object is simple, it has a shortcoming: an event can only be bound to one handler, and the event handler bound later will overwrite the previous one. The bound event handler function. In actual applications, an event from an event source may be processed by multiple functions.

When an event source needs to be processed using multiple functions, you can call addEventListener() (for standard browsers) through the event source to bind the event processing function to achieve this requirement. The way an event source binds multiple event functions through methods is to call addEventListener() multiple times on the event source object, and each call only binds one event processing function.

addEventListener() is a method in the standard event model and is valid for all standard browsers. The format of using addEvent Lister() to bind an event handler is as follows:

事件源.addEventListener(事件名称,事件处理程序,是否捕获);
Copy after login

The parameter "event name" is an event name without "on"; the parameter "whether to capture" is a Boolean value, the default value It is false. When false, event bubbling is achieved, and when true, event capture is achieved.

By calling addEventListener() multiple times, you can bind multiple event handlers to the same event type of an event source object. When an event occurs on an object, all event handling functions bound to the event will be called and executed in sequence in the order in which they were bound. In addition, it should be noted that this in the event handling function bound by addEventListener() points to the event source.

addEventListener() binding handler example:

document.addEventListener('click',fn1,false);//click事件绑定fn1函数实现事件冒泡 document.addEventListener('click',fn2,true);//click事件绑定fn2函数实现事件捕获
Copy after login

[Related recommendations:javascript learning tutorial]

The above is the detailed content of What are the ways to bind events in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!