我在用$(document).ready()的时候,会出现undefined的错误,当然这也不一定是jQuery的bug,也可能是我用的其它库不完善。
跟踪程序后,发现是readyList里有一个元素变成了undefined。
我决定修改jQuery库,因为觉得比较方便。
修改后的代码见下。
可以替换原来的相应部分,或者附在引用标准的jQuery库后以覆盖原来的方法。
jQuery.extend({
isReady: false,
readyList: [],
// Handle when the DOM is ready
ready: function() {
// Make sure that the DOM is not already loaded
if ( !jQuery.isReady ) {
// Remember that the DOM is ready
jQuery.isReady = true;
// If there are functions bound, to execute
if ( jQuery.readyList ) {
// Delete the undefined item!! add by lijs 2009-12-25
for(var i = 0;i < jQuery.readyList.length; i++){
if(jQuery.readyList[i] === undefined){
jQuery.readyList.remove(i);
i--;
}
}
// Execute all of them
jQuery.each( jQuery.readyList, function(){
this.call( document, jQuery );
});
// Reset the list of functions
jQuery.readyList = null;
}
// Trigger any bound ready events
jQuery(document).triggerHandler("ready");
}
}
});
#1
