explain:
Internet
The addEventListener() method is not supported in Explorer 8 and earlier IE versions, and is not supported in Opera 7 and earlier Opera versions. However, for browsers that do not support this function, you can use the attachEvent() method to add event handles.
Internet
Explorer 8 and earlier does not support the removeEventListener() method, Opera
7.0 and earlier versions of Opera are also not supported. However, for those browsers that do not support this function, you can use the detachEvent() method to remove the
The event handle added by the attachEvent() method. (from rookie tutorial above).
keyword:
addEventListener,attachEvent,removeEventListener,detachEvent
JS tool class code:
-
$(function() {
-
-
$.fn.extend({
-
-
-
-
-
-
-
-
addEvent: function(type, handle, bool) {
-
var el, thisLen = this.length;
-
bool ? bool = bool : bool = false;
-
if(thisLen == 1) {
-
el = this[0];
-
el.addEventListener ? el.addEventListener(type, handle, bool) :
-
el.attachEvent('on' + type, handle);
-
} else {
-
for(var i = 0; i < thisLen; i++) {
-
el = this[i];
-
el.addEventListener ? el.addEventListener(type, handle, bool) :
-
el.attachEvent('on' + type, handle);
-
}
-
}
-
},
-
-
-
-
-
-
-
-
-
removeEvent:function(type,handle,bool){
-
var el, thisLen = this.length;
-
console.log(this)
-
bool ? bool = bool : bool = false;
-
if(thisLen == 1) {
-
el = this[0];
-
console.log(el.removeEventListener)
-
el.removeEventListener ? el.removeEventListener(type, handle, bool) :
-
el.detachEvent('on' + type, handle);
-
} else {
-
for(var i = 0; i < thisLen; i++) {
-
el = this[i];
-
el.removeEventListener ? el.removeEventListener(type, handle, bool) :
-
el.detachEvent('on' + type, handle);
-
}
-
}
-
}
-
})
-
})
Call:
-
$('#main').addEvent('touchstart',start);