Home > Web Front-end > JS Tutorial > JS attachEvent, addEventListener study notes under IE and FF_javascript skills

JS attachEvent, addEventListener study notes under IE and FF_javascript skills

WBOY
Release: 2016-05-16 18:41:07
Original
1110 people have browsed it

Object name.addEventListener("Event name (without ON)", function name, true/false); (under FF)
Object name.attachEvent("Event name", function name); (under IE)
Instructions:
For event names, please note that "onclick" should be changed to "click" and "onblur" should be changed to "blur", which means that the event name should not contain "on".
Function name, remember not to follow the parentheses. The last parameter is a Boolean value, indicating the response sequence of the event. Let’s focus on the third parameter (useCapture) of addEventListener. If userCapture is true, the browser uses Capture; if it is false, the browser uses bubbing. It is recommended to use false, let's see an example.
html code


js code

Copy code The code is as follows:

window.onload=function(){ document .getElementById("div_test").addEventListener("click",test1,false); document.getElementById("btn_test").addEventListener("click",test2,false); } function test1(){ alert("Outer layer div trigger") } function test2(){ alert("inner input trigger") }

Experience it yourself. If userCapture is true, test1 will trigger first. If userCapture is false, test2 will trigger first. .

Let’s talk about attachEvent
There’s not much to say about this. I believe everyone is familiar with it. It’s mainly about passing parameters. I’ll talk about it later when I use it, hahaha

Example:
Create binding method:
Copy code The code is as follows:

if (typeof document.addEventListener != "undefined") {
document.addEventListener("mousedown",_lhlclick,true);
} else {
document.attachEvent("onmousedown",_lhlclick);
}

Delete event:
Copy code The code is as follows:

if (typeof document.addEventListener != "undefined") {
document.removeEventListener("mousedown",_lhlclick,true);
} else {
document.detachEvent("onmousedown", _lhlclick);
}
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template