Recently encountered Attachevent and AddEventListener two events, yo, decisive study ~ ~First to popularize the basic knowledge:Attachevent and AddEventListener: Attach other processing events to an event.Online is generally said attachevent for non-Mozilla series, AddEventListener for the Mozilla series.(Mozilla series in the browser, that is, the Firefox browse
This topic describes how to add an event to an element at runtime and add a reference with parameters. Here we use attachevent and addeventlistener
To dynamically add events to a specified element. Specifically, the IE browser and the method used in the browser using the IE kernel (attachevent) are used to dynamically add elements
Process events. Other browsers use the (addeventlistener) method to dynam
There are a lot of blog posts about the differences between addEventListener and attachEvent, but most of them focus on Firefoxchrome. The latter only exists in the IE series and there are many blog posts about the differences between addEventListener and attachEvent, however, most of them focus on Firefox chrome, which only exists in the IE series.
Recently, when I was writing an event proxy, I encountere
Use listening (addEventListener) and AngularJs in angularjs
Usage scope: subclass. js to pass values to the parent class. js or other pages. js
Let alone code first
1. Events. js
angular.module('pr.services') .provider('Notifications', function() { //https://github.com/trochette/Angular-Design-Patterns-Best-Practices/blob/master/js/base/EventDispatcher.js var eventDispatcher = { _listeners: {}, _cache: {}, event: {
AddEventListener has three parameters: the first parameter represents the event name (without on, such as "click"), the second parameter represents the function to receive event handling, and the third parameter is usecapture, which is explained in this article. The above is the code we tested, according to the display of info to determine the order of triggering, there are three AddEventListener, and usec
Yuanwen Http://www.jb51.net/article/32511.htm wrote AddEventListener and attachevent different blog posts, but most of them focus on the former is Firefox chrome, The latter only exists in the IE series write AddEventListener and attachevent differences in a lot of posts, but most of the focus on the former is Firefox Chrome, the latter only exists in the IE series.Recently, when writing an event proxy, I e
But you can get information of the object where the event occured. e. g. you can do stuff dependent on the "ID" of an object, or you can set any additional flags within the object and do stuff dependent on these flags.
Here we have the code for attaching the events:
If (window. addeventlistener) {// Mozilla, Netscape, firefoxobject. addeventlistener ('mouseover', testevent, false); object.
In general we add the event in JS, this is the
1
obj.onclick=method
This way of binding events is compatible with mainstream browsers, but what if one element is added multiple times the same event?
123
obj.onclick=method1;obj.onclick=method2;obj.onclick=method3;
If this is written, then only the last bound event, here is method3 will be executed, this time we can not use the onclick such writing, the protagonist changed the debut,
JS's event binding method, ie only support attachevent, and FF and Chrome only support AddEventListener, so you have to do the two methods compatible, the principle is to first inferattachevent is only true (exists), assuming true is the attachevent () method. Otherwise, use AddEventListener (). also, to avoid inferring each time the event is bound. Ability to encapsulate a function myaddevent (OBJ,EV,FN) {
Original demand: Prevent the button to trigger a click event in a short time, due to repeated submissions caused by the business exception.Figure:DemoDOCTYPE HTML>HTMLLang= "en"dir= "ltr"> Head> MetaCharSet= "Utf-8"> styleMedia= "Screen">. Forbidden Button{cursor:not-allowed; } style> Head> Body> h5>Multiple clicks in 3 seconds only take effect once (based on event interception)h5> Divclass= "Button-group"> Buttonclass= "Link-button"onclick= "Randomnum ()">RandomButto
addeventlistener , literally, is to add a listener to the event, why do you do it? Just to make this happen, we can know in time, along with doing something else. Give a topic: We then give a button on the screen, we hope that when the user moves the mouse, the cursor touches the button, the screen automatically display, "Mouse over!", mouse click, screen then display "Mouse click!", the cursor left, the screen then "mouse left!".Carefully, this is w
When you write JavaScript scripts before, events are implemented using theObject.event = handler;The way you initialize it. This approach is common to Internet Explorer, Mozilla/firefox, and Opera. One problem, however, is that this approach can only have one event for an event-handling process. It is not good if you want an event to execute multiple processes sequentially.However, Internet Explorer provides a attachevent method starting at 5.0, which allows you to assign multiple processes to a
Suppose we need to add a click event handler for a var Aele = document.getElementById (' Aele '); Aele.addeventlistener (' Click ', Function (e) { console.log (e.target); E.preventdefault ();});If we use jquery, we can make it easier:$ (' #aEle '). Click (function (e) { console.log (e.target); E.preventdefault ();});However, the above two code is invalid in the version of IE browser IE8 and below.In IE8 and the following IE browser, we must use E.srcelement to replace the e.target, t
Code
Addeventlistener ----- Firefox
Attachevent --- IE
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> Html Xmlns = "Http://www.w3.org/1999/xhtml" > Head > Meta HTTP-equiv = "Content-Type" Content = "Text/html; charsets = UTF-8" /> Title > Untitled document Title > Script Language = "JavaScript" > Function Test (){ VaR Element = Window.doc ument.
Attachevent ()/addeventlistener ()
Sometimes, when an event of an object is triggered, the program it wants to upload may be a large string, or it may call a function, it is also possible to call another function at the same time. There are many practices in practice. Now I want to introduce them one by one.
Let's take a look at what we often see to do.
1
2
3
4
20
21
Look at this code first.1 function ABC (key) {2 console.log (key); 3 }4for (let i=0;i) {5 oinput[i]. AddEventListener (' Focus ', ABC (i)); 6 }You will find that the focus operation has not yet been performed, and the value of I has been printed sequentially.How to pass the parameter function without being executed immediately, method one: Bind an anonymous function to AddEventListener.Element.addeventlistener (' mouseover ',function(e,some) {f
1, AddEventListenerThe standard method for AddEventListener binding events, such as the following, in which the order of execution and binding order of events is consistent and the order of execution is method1->method2->method3
//element.addEventListener(type,listener,useCapture);
btn1Obj.addEventListener("click",method1,false);
btn1Obj.addEventListener("click",method2,false);
btn1Obj.addEventListener("click",method3,false);
2, Attachevent
Attachevent Method Button onclick
AddEventListener Method Button click
The rationale for the use of the two is that it is not the same as the priority of execution, as the following examples explain:
Attachevent method, attaching additional handling events for an event. (Mozilla series not supported)
AddEventListener Method for Mozilla series
Example:
Java code
Copy Code code as follows:
doc
Can jquery AddEventListener or attachevent be replaced with bind? Meinxiaoo11 class: assembly language was browsed 244 times 2013.11.12Muzi's Sentimental er adoption rate:41% level 11 2013.11.12 Yes. However, for most events there is an easier way to do this than bind, such as the Click event for the click function to bind: $ ("#div"). Click (function () {alert ("clicked!");});AddEventListener or attacheve
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.