gpt4 book ai didi

javascript - 文档就绪与正文就绪与窗口就绪事件

转载 作者:行者123 更新时间:2023-11-30 07:43:34 25 4
gpt4 key购买 nike

这三个事件有什么不同?

哪个在其他之前/之后加载?

   <body> 
<script type='text/javascript'>
$(document).ready(function(){
console.log('Document %s',+new Date());
});
$('body').ready(function(){
console.log('Body %s',+new Date());
});
$(window).ready(function(){
console.log('Window %s',+new Date());
});
</script>
<div>hello world</div>
</body>

奇怪的是,它们的触发顺序与我在代码中输入的顺序相同。对于当前示例。 document 一个先触发,windows 一个在最后触发。

附注我读过 window.onload vs <body onload=""/> , window.onload vs document.onload和其他一些人。

最佳答案

它们完全相同,实际上传递给 $(...) 的参数并不重要。


来自 jQuery documentation about .ready :

.ready( handler )

handler - A function to execute after the DOM is ready.

All three of the following syntaxes are equivalent:

  • $(document).ready(handler)
  • $().ready(handler) (this is not recommended)
  • $(handler)

The .ready() method can only be called on a jQuery object matching the current document, so the selector can be omitted.

正如所见,$() 的参数 (selector) 明确表示是可选的(在执行 $(...).ready 时),因此我们可以放心地假设它与最终结果无关。

关于javascript - 文档就绪与正文就绪与窗口就绪事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11529916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com