Home  >  Article  >  Web Front-end  >  Are the Event objects between js event handling functions congruent_javascript skills

Are the Event objects between js event handling functions congruent_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:08:101084browse

I have encountered this little problem before, but I didn’t summarize it
I encountered it again now. In order to get an accurate conclusion, I recorded it:
For example: Are the Event objects between multiple execution functions of the same event congruent?

For example:

Copy code The code is as follows:

dom.addEvent('click', fna);
dom.addEvent('click',fnb);

1: The attributes added to the Event object in fna are for the Event object in the fnb function executed subsequently Can it be accessed?
2: Is the Event object when executing the fna/fnb function congruent to Eventfna====Eventfnb?

The standard document is too longWhere is the explanation here? What about the rules? You're lazy...

For Jquery, for the same event, the Event objects between multiple execution functions are congruent.
In the implementation of Jquery live binding events, for the function liveHandler that may be executed repeatedly, relying on the liveFired attribute additionally given to the Event object makes the function exit at the beginning of the liveHandler executed after the first time:
Copy code The code is as follows:

if (event.liveFired === this || ...) {
return;
}
///....
event.liveFired = this;

Test:
Copy code The code is as follows:

function a(e) {
e.abc = function() {
alert(a );
};
prevEvent = e;
prevIeEvent=window.event;
}
function b(e,event) {
e.abc(); //fn
alert(e === prevEvent); //true
if(event=window.event){
alert(event===e); //false
alert(event== =prevIeEvent);//false
alert(event===prevEvent);//false
}
}
var t = document.getElementById("p");
if ( t.addEventListener) {
t.addEventListener('click', a, false);
t.addEventListener('click', b, false)
} else {
t.attachEvent(' onclick', b);
t.attachEvent('onclick', a)
}

For the native binding event method [addEventListener, attachEvent], for multiple execution functions The Event objects (events passed through parameters) are congruent. In IE, the Event objects obtained through window.event are not congruent. It is not equivalent to the Event passed through parameters.
For bubbling events:
Copy code The code is as follows:

dom.addEvent( 'click',fna);
domParentNode.addEvent('click',fnb);

In jquery, when an event is triggered in the form of trigger, the function between the bubbling events Event objects are congruent. The events triggered by actual user actions are not the same object. Custom attributes cannot be passed either. None of the value settings affect the real originalEvent.
Use native binding event method [addEventListener,attachEvent]:
Copy code The code is as follows:

function a(e) {
e.abc = function() {
alert(a);
};
prevEvent = e;
prevIeEvent=window.event;
}
function b(e,event) {
alert(e.abc); //fn
alert(e === prevEvent); //true
if(event=window .event){
alert(event===e); //false
alert(event===prevIeEvent);//false
alert(event===prevEvent);//false
}
}
var t = document.getElementById("p");
if (t.addEventListener) {
t.addEventListener('click', a, false);
document.body.addEventListener('click', b, false);
} else {
t.attachEvent('onclick', a);
document.body.attachEvent('onclick', b );
}

The result is that the Event objects within the function between the bubbling events in IE are inconsistent when attachEvent is bound to the event. FF, etc. are congruent objects with addEventListener.
In fact, in non-IE, the processing functions between event object Propagation, or the Event objects between multiple functions triggered by the same DOM node are congruent. It has nothing to do with the binding form [addEventListener/DOM0].
In IE, only the Event objects (attachEvent) passed through parameters between multiple functions triggered by the same DOM node are congruent.
Statement:
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