The differences between attachEvent and addEventListener adapt to different browser versions. during use, pay attention to the onclick button of the attachEvent method.
AddEventListener method button click
The usage principle of the two is as follows: the priority of the execution can be different. The example below is as follows:
The attachEvent method attaches other processing events to an event. (Mozilla
Attachevent method button onclick
Addeventlistener method button click
The usage principle of the two is as follows: the priority of the execution can be different. The example below is as follows:
The attachevent method attaches other processing events to an event. (Mozilla series not supported)
The addeventlistener method is used in the Mozilla series.
Example:
Java
Code
Copy code The Code is as follo
If we want to define a carriage return event, we must bind the event like jquery. We use addEventListener and attachEvent to bind the event in pure js. They do not use ie or ff browsers respectively.
For example, bind a carriage return event.
The Code is as follows:
Copy code
$ (Function (){// Define the carriage return eventIf (document. addEventListener) {// if it is FirefoxDocumen
The attachEvent method attaches other processing events to an event. (Mozilla series not supported)
The addEventListener method is used in the Mozilla series.
Example:Document. getElementById ("btn"). onclick = method1;
Document. getElementById ("btn"). onclick = method2;
Document. getElementById ("btn"). onclick = method3;
If this is the case, only medhot3 will be executed.
Written as follows: var btn1Obj = document. getElementById ("btn1 ");// Obje
AttachEvent method button onclickAddEventListener method button clickThe usage principle of the two is as follows: the priority of the execution can be different. The example below is as follows:The attachEvent method attaches other processing events to an event. (Mozilla series not supported)The addEventListener method is used in the Mozilla series.Example:Java codeCopy codeThe Code is as follows:Document. getElementById ("btn"). onclick = method1;Do
Attachevent ()/addeventlistener () object addition trigger event
Sometimes, when an event of an object is triggered, the program to be executed may be a large string, or it may call a function, it is also possible to call another function at the same time.
Document. getelementbyid ("BTN"). onclick = Method1;Document. getelementbyid ("BTN"). onclick = method2;Document. getelementbyid ("BTN"). onclick = method3;If this is the case, only medhot3 will be
UseCapture of the three parameters of addEventListener
This article describes how to use and share the useCapture parameter among the three parameters of addEventListener. For more information, see
AddEventListener has three parameters: the first parameter indicates the event name (not including on, such as "click"); the second parameter indicates the function
You may have encountered this situation, that is, in JS
Code When window. onload is used, the onload event in the body may be affected. You can write all the data in the body or in window. onload, but this is not very convenient. Sometimes we need to use both of them at the same time. In this case, we need to use window. attachevent and window. addeventlistener to solve the problem.
The following is a solution. For the usage of attachevent and
Let's talk about js capture and bubbling from a simple example, and talk about js capture and bubbling.
Event bubbling and event capture. Below is an example of capturing and bubbling in js:Html structure:
Css style control:
#tianzi{ width: 400px; height: 400px; background: red;}#tianzi1{ width: 300px; height: 300px; background: yellow;}#tianz
This article describes how to use and share the useCapture parameter among the three parameters of addEventListener. For more information, see addEventListener with three parameters: the first parameter indicates the event name (not including on, such as "click"), the second parameter indicates the function for Receiving Event processing, and the third parameter is useCapture, which will be explained in thi
This article describes how to use and share the useCapture parameter among the three parameters of addEventListener. For more information, see addEventListener with three parameters: the first parameter indicates the event name (not including on, such as "click"), the second parameter indicates the function for receiving event processing, and the third parameter is useCapture, which will be explained in thi
About the compatibility of attachevent and AddEventListener in native event bindings and the issue of this reference window in attachevent functionsby OdacashAt 2014-07-0129 Read0 replies.0.0 Greek dollarsClick on the " Accept as Answer" button in the answer below and select the contribution (Nozomi) ratio and click on the "Confirm Checkout" button.
Total 0 people
0%
Abo
This article mainly introduces document. addEventListener. The following is an example. You can refer to document. addEventListener ("event name", function, false );
The Code is as follows:
Function a function (event ){// Method execution}
The last parameter in addEventListener determines the Event Response sequence;
If the execution order of the true e
Now jquery is more popular, so we use more is jquery, rarely with the native JS. But still need to understand the drop.The code is as follows:If we're going to bind three functions to the button's Click button, it's easier for a person with less knowledge of JS to write this:This is wrong because the click event will only execute function 3. So we need to use the attachevent and AddEventListener events, attachevent only for IE browser, and
W3C Dom standard binding event with addeventlistener
Use attachevent to bind events to the DOM standard of IE
/**
* Bind events
*/
Function addevent (ELEM, EVT, FN, B ){
If (ELEM. addeventlistener ){
If (4 = arguments. Length ){
ELEM. addeventlistener (EVT, FN, B );
}
Else {
ELEM. addeventlistener (EVT, FN, false );
}
Code$ (function () { $ ("#btn"). Click (function () { Console.log (2) }) $ ("#btn"). Click (function () { Console.log (1) }) $ ("#btn"). Click (function () { Console.log (3) })})The result of this click is output 2,1,3Because you are here to bind the click, he still has the first event before you can unbind it, so he will continue the previous one;Nativedocument.getElementById ("Btn"). AddEventListener ("click",
function true)The last parameter in the AddEventListener determines the order in which the event responds;If true, the event is executed in the order of AddEventListener----The onclick event of the label----Document.onclickIf the order of the event is false for the OnClick event of the label----Document.onclick----AddEventListenerMouse eventsDocument.addeventlistener (' MouseDown ', MouseDown); Document.add
the difference between attachevent and AddEventListener
Adapt to different browser versions, while using the process to pay attention toAttachevent Method Button onclickAddEventListener Method Button clickThe principle of use: You can operate on events that do not have the same priority:The Attachevent method, which attaches additional processing events to an event. (Mozilla series not supported)AddEventListen
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.