Detailed explanation of js event monitoring examples

小云云
Release: 2023-03-21 08:14:02
Original
1882 people have browsed it

Definition of event monitoring

In Javascript, browsers are generally divided into two categories:

① Browsers based on IE kernel (IE browser with version number less than 9 browser)

② Browser based on W3C kernel (IE browser, Firefox, Google and other browsers with version number greater than 9)

1) Basic syntax: Browser based on IE kernel

dom object.attachEvent(type, callback, capture): Bind an event listener to the element

Parameter description:

type: The bound event type, such as onclick, onmouseover , onmouseout

callback: event handler, usually an anonymous function

capture: the browser model used, bubbling model and capture model. By default, browsers below IE8 only support bubbling. Bubble model!

?

##2,
##1

2

3

4

5

6

7

8

9

10

11

12

##

//封装$函数,用于获取id的元素 function $(id){ return document.getElementById(id) } //绑定事件监听 $('btn').attachEvent('onclick',function(){ alert('hello') }); //二次绑定 $('btn').attachEvent('onclick',function(){ alert('world') });
Copy after login


Basic syntax: event monitoring based on W3C kernel

dom object.addEventListener(type, callback): Bind event listening for W3C kernel browser

Parameter description:

type: Bind event type, without 'on' prefix, such as click, mouseover, mouseout

callback: event handler, usually an anonymous function



Summary of the differences in event monitoring

The listening method of the IE kernel and the listening method of the W3C kernel:

①The method is different

The IE kernel’s listening method The browser uses attachEvent for binding

W3C core browser uses addEventListener for binding

②The parameters are different

IE core browser has three binding methods. Parameters type, callback, capture (browser model used)

W3C kernel browser, its binding method has two parameters in total, type and callback

③type parameters are different

For IE core browsers, type does not need to be prefixed with 'on', such as onclick

W3C core browsers, type does not need to be prefixed with 'on', such as click

④The triggering order is different

In browsers with IE core, the event monitoring is first bound and then triggered, and in the latter binding, it is triggered first.

In browsers with W3C kernel, the event monitoring is first Binding triggers first, binding triggers later

Related recommendations:

Detailed explanation of event monitoring and event publishing usage examples in Node.js

Detailed introduction to JS key combination event listening plug-in

Detailed explanation of jQuery-mobile event monitoring and usage

1
2

3

4

5

6

7

8

9

10

11

12

//封装$函数,用于获取id的元素 function $(id){ return document.getElementById(id) } //绑定事件监听 $('btn').addEventListener('onclick',function(){ alert('hello') }); //二次绑定 $('btn').addEventListener('onclick',function(){ alert('world') });
Copy after login


The above is the detailed content of Detailed explanation of js event monitoring examples. For more information, please follow other related articles on the PHP Chinese website!

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!