Home > Web Front-end > JS Tutorial > body text

Analyze the difference between Js on and addEventListener principles and usage

coldplay.xixi
Release: 2020-07-13 17:22:47
forward
2550 people have browsed it

Analyze the difference between Js on and addEventListener principles and usage

1. First introduce the usage of the two:

1. Usage of on: Take onclick as an example

The first one:

obj.onclick = function(){
//do something..
}
Copy after login

The second one:

obj.onclick= fn;
function fn (){
//do something...
}
Copy after login

Third method: Use an anonymous function to pass parameters when function fn has parameters:

obj.onclick = function(){fn(param)};
function fn(param){
//do something..
}
Copy after login

cannot be written like this: Wrong writing: obj.onclick= fn(param):

Because the function written in this way will be executed immediately and will not wait for the click to trigger, pay special attention to

2. The usage of addEventListener:

Format:

addEventListener(event,funtionName,useCapture)
Copy after login

Parameters:

  • event: type of event such as "click"
  • funtionName: method name
  • useCapture( Optional): Boolean value that specifies whether the event is executed in the capture or bubbling phase.
  • true - The event handler is executed during the capture phase
  • false- false- Default. The event handle is executed in the bubbling phase

Writing method:

The first type:

obj.addEventListener("click",function(){
//do something
}));
Copy after login

The second type, you can directly write the function name without parameters

obj.addEventListener("click",fn,fasle));
function fn(){
//do something..
}
Copy after login

Third type: When the function has parameters, you need to use an anonymous function to pass the parameters

obj.addEventListener("click",function(){fn(parm)},false);
Copy after login

2. The difference between the two

1. The on event will be overwritten by the subsequent on event

Take onclick as an example:

//obj是一个dom对象,下同//注册第一个点击事件
obj.onclick(function(){
alert("hello world");
});
//注册第二个点击事件
obj.onclick(function(){
alert("hello world too");
});
Copy after login

In the end there will be only Pop-up box output:

hello world too
Copy after login

2.addEventListener will not be overwritten.

//注册第一个点击事件
obj.addEventListener("click",function(){
alert("hello world");
}));
//注册第二个点击事件
obj.addEventListener("click",function(){
alert("hello world too");
}));
Copy after login

This will output continuously:

hello world
hello world too
Copy after login

3. Notes on addEventListener:

1. Special note that addEventListener is not compatible with IE9 and below. For IE9 and below, use attachEvent()

obj.attachEvent(event,funtionName);
Copy after login

Parameters:

event: event type (needs to be written as "onclick "Add on in front, this is different from addEventListener)

funtionName: method name (if you want parameters, you also need to use anonymous functions to pass parameters)

4. Event collection:

1. Mouse event:

  • click (click)
  • dbclick (double-click)
  • mousedown(mouse down)
  • mouseout(mouse removed)
  • mouseover(mouse moved in)
  • mouseup(mouse up)
  • mousemove(mouse movement)

2. Keyboard events:

  • keydown(key press)
  • keypress(key press)
  • keyup(keyup)
  • 3.HTML event:
  • load(load page)
  • unload(unload the page)
  • change( Change content)
  • scroll(scroll)
  • focus(get focus)
  • blur(lose focus)

5. Summary:

onXXX and addEventListener both add event listeners to dom elements so that they can execute corresponding code and operations after the event occurs. With them we realize page and user interaction.

Related learning recommendations: javascript video tutorial

The above is the detailed content of Analyze the difference between Js on and addEventListener principles and usage. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js on
source:jb51.net
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
Popular Tutorials
More>
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!