gpt4 book ai didi

javascript - 来自 W3C 验证器的错误 “Element script must not have attribute defer unless attribute src is also specified”

转载 作者:行者123 更新时间:2023-11-27 22:47:48 25 4
gpt4 key购买 nike

我有一个 HTML 文档,其源代码有一个 script 元素,该元素具有 defer 属性。 W3C 验证器说它无效,因为它缺少 src 属性,但实际上源代码中有一个 src 属性:

<script defer type="text/javascript">if($(window).width()>1024){document.write("<"+"script src='js/jquery.preloader.js'></"+"script>");}</script>

我能做什么?

最佳答案

这里是 W3C HTML 检查器(验证器)的维护者。

修复方法:将脚本内容放入单独的文件中,并使用 src 属性来引用它。

检查器报告问题中代码片段的错误,因为 HTML 规范用于明确声明 script 不能具有 defer 属性,除非它还具有src 属性。

规范不再明确指出该限制,但我认为这只是 unintentional regression caused by a later spec change (你的问题有助于理解这一点——所以,谢谢!)。

因此,我将调查并提出规范修复的拉取请求,然后稍后更新此答案。

2018-05-05更新

在此郑重声明,我最终确实提交了带有 HTML 规范补丁的拉取请求:

https://github.com/whatwg/html/pull/2509

...并且已合并到 HTML 规范源中:

https://github.com/whatwg/html/commit/3c5180a08f90a375c64f8191f32f8c7ddfec0ba3

…所以现在当前的 HTML 规范再次规定了限制:

https://html.spec.whatwg.org/multipage/scripting.html#attr-script-defer

The async and defer attributes are boolean attributes that indicate how the script should be evaluated. Classic scripts may specify defer or async, but must not specify either unless the src attribute is present.

关于javascript - 来自 W3C 验证器的错误 “Element script must not have attribute defer unless attribute src is also specified”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38291503/

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