添加到 HurnsMobile 的出色答案;看看bindReady()
,这是 jQuery 每次调用时绑定到文档加载事件的内部调用,$(some_function)
或者$(document).ready(some_function)
我们看到为什么我们不能绑定到"ready"
:
bindReady: function() {
if ( readyBound ) {
return;
}
readyBound = true;
// Catch cases where $(document).ready() is called after the
// browser event has already occurred.
if ( document.readyState === "complete" ) {
return jQuery.ready();
}
// Mozilla, Opera and webkit nightlies currently support this event
if ( document.addEventListener ) {
// Use the handy event callback
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
// A fallback to window.onload, that will always work
window.addEventListener( "load", jQuery.ready, false );
// If IE event model is used
} else if ( document.attachEvent ) {
// ensure firing before onload,
// maybe late but safe also for iframes
document.attachEvent("onreadystatechange", DOMContentLoaded);
// A fallback to window.onload, that will always work
window.attachEvent( "onload", jQuery.ready );
// If IE and not a frame
// continually check to see if the document is ready
var toplevel = false;
try {
toplevel = window.frameElement == null;
} catch(e) { //and silently drop any errors
}
// If the document supports the scroll check and we're not in a frame:
if ( document.documentElement.doScroll && toplevel ) {
doScrollCheck();
}
}
}
总而言之,$(some_function)
调用一个绑定到的函数:
- DOMContentLoaded
- onreadystatechange (DOMContentLoaded)
- window.load / onload
您最好的选择是绑定到那些可能创建新.tooltipper
元素的操作,而不是尝试侦听 ready 事件(仅发生一次)。