在包含脚本标签的Ajax调用中,所有发送的脚本都会在插入到文档对象模型后执行。
但是在您的情况下,脚本标记包含事件分配,因此事件分配将执行,但是为什么在插入到文档对象模型后执行分配的函数而未引发就绪事件?
发生这种情况的原因是:
总结:
Ready 仅类似于 event 但不是真正的一个 event,它是 Pseudo-Event。
详细
以下代码是jquery库的来源:
jQuery.fn.ready = function( fn ) {
// Add the callback
jQuery.ready.promise().done( fn );
return this;
};
你看到 ready 是一个获取函数的 jQuery 插件。在函数体中说当就绪事件完成时执行发送函数。但是当就绪时完成。再次查看 JQuery 源代码:
jQuery.extend( {
// Is the DOM ready to be used? Set to true once it occurs.
isReady: false,
// A counter to track how many items to wait for before
// the ready event fires. See #6781
readyWait: 1,
// Hold (or release) the ready event
holdReady: function( hold ) {
if ( hold ) {
jQuery.readyWait++;
} else {
jQuery.ready( true );
}
},
// Handle when the DOM is ready
ready: function( wait ) {
// Abort if there are pending holds or we're already ready
if ( wait === true ? --jQuery.readyWait : jQuery.isReady ) {
return;
}
// Remember that the DOM is ready
jQuery.isReady = true;
// If a normal DOM Ready event fired, decrement, and wait if need be
if ( wait !== true && --jQuery.readyWait > 0 ) {
return;
}
// If there are functions bound, to execute
readyList.resolveWith( document, [ jQuery ] );
}
} );
加载文档对象模型的以下行集已完成:
// Remember that the DOM is ready
jQuery.isReady = true;
所以如果 Jquery.isReady == true,传递给 ready 方法的函数会立即执行。
所以为在就绪状态下运行的脚本提供了就绪方法,而不是就绪事件。