缓存机制和事件触发
jquery的缓存机制带来何种收益,现在还没找到。
但若使用$.extend($.noData,{'td':true,'tr':true});后,如下语句:
$("#test").click(function(e){alert("d")});
将不会再触发click事件。
分析如下:
line3296中:
jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
"mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
"change select submit keydown keypress keyup error").split(" "), function( i, name )
函数中:
this.bind( name, data, fn ) 。
line3032行:
handler = ......return fn.apply( this, arguments );
for ( var i = 0, l = this.length; i < l; i++ ) {
jQuery.event.add( this[i], type, handler, data );
line2147:
jQuery.event = {
add:
// Init the element's event structure
var elemData = jQuery._data( elem );
// If no elemData is found then we must be trying to bind to one of the
// banned noData elements
if ( !elemData ) {
return;
}
这里如果elemData为false,也就是说没有加入cache,则返回了,不会执行至:
if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) {
// Bind the global event handler to the element
if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle, false );
} else if ( elem.attachEvent ) {
elem.attachEvent( "on" + type, eventHandle );
}
}
可以看到无非也是使用了
addEventListener 和 attachEvent来解决兼容性
阅读(1007) | 评论(1) | 转发(0) |