gpt4 book ai didi

javascript - 此处文档类型不允许元素 "ul"

转载 作者:太空宇宙 更新时间:2023-11-04 05:13:09 25 4
gpt4 key购买 nike

我正在使用 XHTML 1.0 严格文档类型。我在页面末尾有一些 JavaScript:

$.each(x, function(k, v){
txt += '<li>';
txt += '<strong>' + v.description + '</strong>: $' + v.postage;
txt += '</li>';

当我在 W3.org 上验证我的文档时,出现以下错误:

txt += '<ul><li>';

The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).

我是 JavaScript 的初学者,如果有人可以帮助我修复我页面上的这个验证错误,那就太好了。

最佳答案

原因是您位于仍然由 XML 解析器解析的内联脚本元素中(理论上)。您在 JavaScript 字符串中的标记可能被计为元素。

一个解决方案(最好的解决方案,您应该已经在做)是简单地将您的 JavaScript 移动到一个外部文件并链接到它:

<script type="text/javascript" src="my/script.js"> </script>

其他涉及转义或使用 <![CDATA[ block ,并且通常令人不快。

关于javascript - 此处文档类型不允许元素 "ul",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8827230/

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