Home >Web Front-end >JS Tutorial >Some compatible writing methods for handling event binding in javascript_javascript skills

Some compatible writing methods for handling event binding in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:38:24958browse

Bind event

Copy code The code is as follows:

var addEvent = function( obj, type, fn ) {
if (obj.addEventListener)
obj.addEventListener( type, fn, false );
else if (obj.attachEvent) {
obj["e" type fn] = fn ;
obj.attachEvent( "on" type, function() {
obj["e" type fn]();
} );
}
};

Another implementation
Copy code The code is as follows:

var addEvent = ( function () {
if (document.addEventListener) {
return function (el, type, fn) {
el.addEventListener(type, fn, false);
};
} else {
return function (el, type, fn) {
el.attachEvent('on' type, function () {
return fn.call(el, window.event);
} ; It is triggered when attributes change. Common ones include text length changes, sample length changes, etc. FF's roughly similar attribute is the oninput event, but it only targets the value attributes of textfield and textarea. Safari, Firefox, Chrome and Opera all support this attribute.



Copy code


The code is as follows:
var addPropertyChangeEvent = function (obj,fn) { if(window.ActiveXObject){ obj.onpropertychange = fn; }else{ obj.addEventListener("input",fn,false); }
}


Remove event




Copy code

The code is as follows:
var removeEvent = function( obj, type, fn ) { if (obj.removeEventListener) obj.removeEventListener( type, fn, false ); else if (obj.detachEvent) { obj.detachEvent( "on" type, obj["e" type fn] ); obj["e" type fn] = null;
}
};


Loading event




Copy code

The code is as follows:
var loadEvent = function(fn) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = fn; }else { window.onload = function() {
oldonload();
fn();
}
}
}


Block event




Copy Code

The code is as follows:
var stopEvent = function(e){ e = e || window.event; if(e.preventDefault ) { e.preventDefault(); e.stopPropagation(); }else{
e.returnValue = false;
e.cancelBubble = true;
}
}


If you just want to prevent event bubbling




Copy the code

The code is as follows:
var stopPropagation = function(e) { e = e || window.event; if (! "v1") { e.cancelBubble = true; } else { e.stopPropagation();
}
}


Get the event source object

Equivalent to Event.element(e of the Prototype.js framework )



Copy code


The code is as follows:
Or this function is more powerful , I developed it when developing datagrid. For detailed usage, see "Teach you step by step how to implement table sorting (Part 2)".




Copy code

The code is as follows:

var getEvent = function(e) {
var e = e || window.event;
if (!e) {
var c = this.getEvent.caller;
while (c) {
e = c.arguments[0];
if (e && (Event == e.constructor || MouseEvent == e.constructor)) {
break;
}
c = c.caller;
}
}
var target = e.srcElement ? e.srcElement : e.target,
currentN = target.nodeName.toLowerCase(),
parentN = target.parentNode.nodeName.toLowerCase(),
grandN = target.parentNode.parentNode.nodeName.toLowerCase ();
return [e,target,currentN,parentN,grandN];
}

最后附上DOM3.0事件的一览表
type Bubbling phase Cancelable Target node types DOM interface
DOMActivate Yes Yes Element UIEvent
DOMFocusIn Yes No Element UIEvent
DOMFocusOut Yes No Element UIEvent
focus No No Element UIEvent
blur No No Element UIEvent
textInput Yes Yes Element TextEvent
click Yes Yes Element MouseEvent
dblclick Yes Yes Element MouseEvent
mousedown Yes Yes Element MouseEvent
mouseup Yes Yes Element MouseEvent
mouseover Yes Yes Element MouseEvent
mousemove Yes Yes Element MouseEvent
mouseout Yes Yes Element MouseEvent
keydown Yes Yes Element KeyboardEvent
keyup Yes Yes Element KeyboardEvent
mousemultiwheel Yes Yes Document, Element MouseMultiWheelEvent
mousewheel Yes Yes Document, Element MouseWheelEvent
DOMSubtreeModified Yes No Document, DocumentFragment, Element, Attr MutationEvent
DOMNodeInserted Yes No Element, Attr, Text, Comment, CDATASection, DocumentType, EntityReference, ProcessingInstruction MutationEvent
DOMNodeRemoved Yes No Element, Attr, Text, Comment, CDATASection, DocumentType, EntityReference, ProcessingInstruction MutationEvent
DOMNodeRemovedFromDocument No No Element, Attr, Text, Comment, CDATASection, DocumentType, EntityReference, ProcessingInstruction MutationEvent
DOMNodeInsertedIntoDocument No No Element, Attr, Text, Comment, CDATASection, DocumentType, EntityReference, ProcessingInstruction MutationEvent
DOMAttrModified Yes No Element MutationEvent
DOMCharacterDataModified Yes No Text, Comment, CDATASection, ProcessingInstruction MutationEvent
DOMElementNameChanged Yes No Element MutationNameEvent
DOMAttributeNameChanged Yes No Element MutationNameEvent
load No No Document, Element Event
unload No No Document, Element Event
abort Yes No Element Event
error Yes No Element Event
select Yes No Element Event
change Yes No Element Event
submit Yes Yes Element Event
reset Yes Yes Element Event
resize Yes No Document, Element UIEvent
scroll Yes No Document, Element UIEvent
作者:Ruby's Louvre
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