gpt4 book ai didi

javascript - 延迟 javascript - 什么是正确的 html 语法 defer 或 defer ="defer"

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

在 javascript 中使用 defer 属性的正确语法是什么?

我见过有两种方式:

1:

<script defer  >...</script>

2:

<script defer="defer">...</script>

根据经验 [和我找不到的引用资料],我更倾向于使用第二个选项,但我只是仔细检查了 official w3c site看来选项 1 是正确的。

谢谢

最佳答案

defer 是一个 boolean attribute [HTML 4.01 spec] :

Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".

[...]

In HTML, boolean attributes may appear in minimized form -- the attribute's value appears alone in the element's start tag. Thus, selected may be set by writing:

<OPTION selected>

instead of:

<OPTION selected="selected">

Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form.

但是,如果您使用 XHTML,则必须使用第二种形式,因为 XHTML 遵循 XML 语法,其中属性始终必须具有值。

关于javascript - 延迟 javascript - 什么是正确的 html 语法 defer 或 defer ="defer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17169123/

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