读jQuery之十三 添加事件和删除事件的核心方法_jquery_脚本之家

jQuery的事件模块严重依赖于其数据储存,你会发现我的代码中的dataManager对象对应它。
这里只提供bind和unbind方法。暂不包含 1, 事件命名空间 2, 事件代理 3,
特殊事件如dom ready 接口如下: 复制代码
代码如下: E.bind; E.bind(el, ‘click’, fn, data); E.unbind; E.unbind;
E.unbind; 复制代码 代码如下: /** *
Event from jQuery * 2011-06-20 snandy * * A number of helper
functions used for managing events. * Many of the ideas behind this
code originated from jQuery library . * * example * * E.bind; * *
E.bind(el, ‘click’, fn, data); * * E.unbind; * * E.unbind; * *
E.unbind; * */ E = function { var uuid = 0, globalCache = {}, doc =
window.document, w3c = !!doc.addEventListener, expando = ‘snandy’ +
.replace, addListener = w3c ? function { el.addEventListener; } :
function { el.attachEvent; }, removeListener = w3c ? function {
el.removeEventListener; } : function { el.detachEvent; }; dispatch = w3c
? function{ try{ var evt = doc.createEvent; evt.initEvent;
el.dispatchEvent{ alert }; } : function{ try{ el.fireEvent; }catch; } },
dataManager = { data : function { var getByName = typeof name ===
“string”, thisCache, isNode = elem.nodeType, cache = isNode ?
globalCache : elem, id = isNode ? elem[ expando ] : elem[ expando ]
&& expando; if { elem[expando] = id = ++uuid; } if{ cache[id] = {};
} thisCache = cache[id]; if { thisCache[name] = data; } return
getByName ? thisCache[name] : thisCache; }, removeData : function {
var id = elem[expando], thisCache = globalCache[id]; if{ return; }
if(typeof name === ‘string’) { delete thisCache[name]; }else{ delete
globalCache[id]; } } }; function returnFalse() { return false; }
function returnTrue() { return true; } function now.getTime(); }
function isEmptyObject{ for{ return false; } return true; } function
addEvent (elem, types, handler, data) { if ( elem.nodeType === 3 ||
elem.nodeType === 8 ) { return; } if { handler = returnFalse; } else if
{ return; } var elemData = dataManager.data, events = elemData.events,
eventHandle = elemData.handle, types = types.split { elemData.events =
events = {}; } if { elemData.handle = eventHandle = function { return
evtHandle.call( eventHandle.elem, e ); }; } eventHandle.elem = elem; var
type, i = 0; while { var handleObj = {handler : handler, data : data},
handlers = events[type]; if { handlers = events[type] = [];
addListener( elem, type, eventHandle ); } handlers.push; } elem = null;
} function evtHandle { event = fixEvent( event || window.event ); var
handlers = ((dataManager.data || {})[event.type] || []).slice;
event.currentTarget = this; for( var j = 0, l = handlers.length; j <
l; j++ ) { var handleObj = handlers[j]威斯尼斯人官网,; event.handler =
handleObj.handler; event.data = handleObj.data; event.handleObj =
handleObj; var ret = handleObj.handler.call; if { if {
event.preventDefault(); event.stopPropagation(); } } if(
event.isImmediatePropagationStopped { break; } } } function removeEvent(
elem, types, handler ) { // don’t do events on text and comment nodes
if( elem.nodeType === 3 || elem.nodeType === 8 ) { return; } if {
handler = returnFalse; } var type, origType, i = 0, j, elemData =
dataManager.data, events = elemData && elemData.events; if( !elemData ||
!events ) { return; } // Unbind all events for the element if { types =
types || “”; for { removeEvent; } return; } // Handle multiple events
separated by a space // jQuery.unbind(“mouseover mouseout”, fn); types =
types.split; while( ) { origType = type; handleObj = null; eventType =
events[ type ]; if { continue; } if { for ( j = 0; j <
eventType.length; j++ ) { handleObj = eventType[ j ]; removeEvent(
elem, origType, handleObj.handler ); eventType.splice; } continue; }
for( j = 0; j < eventType.length; j++ ) { handleObj = eventType[ j
]; if( handler === handleObj.handler ) { // remove the given handler
for the given type eventType.splice; } } } // remove generic event
handler if no more handlers exist if ( eventType.length === 0 ) { delete
events[ origType ]; } // Remove the expando if it’s no longer used if
( isEmptyObject { var handle = elemData.handle; if { handle.elem = null;
} delete elemData.events; delete elemData.handle; if ( isEmptyObject {
dataManager.removeData; } } } function Event { this.originalEvent = src;
this.type = src.type; this.timeStamp = now(); } Event.prototype = {
preventDefault: function() { this.isDefaultPrevented = returnTrue; var e
= this.originalEvent; if { e.preventDefault(); } e.returnValue = false;
}, stopPropagation: function() { this.isPropagationStopped = returnTrue;
var e = this.originalEvent; if { e.stopPropagation(); } e.cancelBubble =
true; }, stopImmediatePropagation: function() {
this.isImmediatePropagationStopped = returnTrue; this.stopPropagation();
}, isDefaultPrevented: returnFalse, isPropagationStopped: returnFalse,
isImmediatePropagationStopped: returnFalse }; function fixEvent { var
props = “altKey attrChange attrName bubbles button cancelable charCode
clientX clientY ctrlKey currentTarget data detail eventPhase fromElement
handler keyCode layerX layerY metaKey newValue offsetX offsetY
originalTarget pageX pageY prevValue relatedNode relatedTarget screenX
screenY shiftKey srcElement target toElement view wheelDelta
which”.split, len = props.length; var originalEvent = evt; evt = new
Event; for(var i = len, prop; i;) { prop = props[ –i ]; evt[ prop ]
= originalEvent[ prop ]; } if { evt.target = evt.srcElement ||
document; } if( evt.target.nodeType === 3 ) { evt.target =
evt.target.parentNode; } if( !evt.relatedTarget && evt.fromElement ) {
evt.relatedTarget = evt.fromElement === evt.target ? evt.toElement :
evt.fromElement; } if( evt.pageX == null && evt.clientX != null ) { var
doc = document.documentElement, body = document.body; evt.pageX =
evt.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) –
(doc && doc.clientLeft || body && body.clientLeft || 0); evt.pageY =
evt.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) –
(doc && doc.clientTop || body && body.clientTop || 0); } if( !evt.which
&& ((evt.charCode || evt.charCode === 0) ? evt.charCode : evt.keyCode) )
{ evt.which = evt.charCode || evt.keyCode; } if( !evt.metaKey &&
evt.ctrlKey ) { evt.metaKey = evt.ctrlKey; } if( !evt.which &&
evt.button !== undefined ) { evt.which = (evt.button & 1 ? 1 : (
evt.button & 2 ? 3 : ( evt.button & 4 ? 2 : 0 ) )); } return evt; }
function bind { var handler; if( typeof type === “object” ) { for {
bind(el, key, type[读jQuery之十三 添加事件和删除事件的核心方法_jquery_脚本之家。key], data); } return; } handler = fn; addEvent(
el, type, handler, data ); } function unbind { if( typeof type ===
“object” ) { for { unbind; } }else { removeEvent; } } return { data :
dataManager.data, removeData : dataManager.removeData, bind : bind,
unbind : unbind }; };

发表评论

电子邮件地址不会被公开。 必填项已用*标注

相关文章