gpt4 book ai didi

Javascript [defer] 属性和 document.ready?

转载 作者:搜寻专家 更新时间:2023-11-01 04:10:19 25 4
gpt4 key购买 nike

在阅读 mdn 中的 defer 属性后

This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed.

看起来不错。

所以我有 tested它针对 $(function () { });$(window).load(...)

<script>
$(function ()
{
alert('1')
});
$(window).load(function ()
{
alert('2')
});
</script>

<script defer="defer">
alert('4');
</script>

这段代码总是输出4,1,2!

好的,现在我可以识别文档被解析的时间了。

在什么情况下我需要 document.ready before 阶段(解析时间完成的地方)?

最佳答案

来自 MDN

The defer attribute shouldn't be used on scripts that don't have the src attribute

实际用途是您仍然可以在页面顶部放置脚本,并让浏览器在整个页面完全解析后加载它们,从而提高客户端的性能。

来自 YSlow

The DEFER attribute indicates that the script does not contain document.write, and is a clue to browsers that they can continue rendering

关于Javascript [defer] 属性和 document.ready?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12803203/

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