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

标签 javascript html w3c

在 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/

相关文章:

css - 仅创建空 div 的索引页(不显示,即使在定义高度/宽度之后)

javascript - 返回 JS 数组中最大的电话号码

javascript - SELECT with 2 optgroups build with KnockOut 将值重置为空

c# - 按钮变灰,直到所有文本框都包含 ASP 中的文本

javascript - 如何在 HTML5 的 Canvas 中复制形状?

javascript - 如何在没有 JavaScript 的情况下运行我的搜索框?

javascript - 将 Div 置于完全独立的 div 之上

css - 根据 W3C,-negative margin 或 padding 是无效的 CSS 吗?

w3c - CSS 折叠边距有什么意义?

web - 具有凭据和 "essentially"任何来源的跨源资源共享