只是一个快速验证问题,我得到:
文档类型不允许此处使用元素“div”
我的代码:
$('#wrapper').prepend('<div id="slider"></div>');
这只是一个 JS 的东西,我该如何处理?
最佳答案
除非您使用 XHTML,否则我想不出任何时候您会在 JavaScript 中间遇到该错误,在这种情况下,简短的答案是:停止使用 XHTML,开始使用 HTML 4 或5.
您收到错误的原因是 described in the XHTML 1.0 specification :
In XHTML, the script and style elements are declared as having
#PCDATA
content. As a result,<
and&
will be treated as the start of markup
最新版本XHTML Media Types note提供解决方法:
DO use external scripts if your script uses
<
or&
or]]>
or--
. DO NOT embed a script in a document if it contains any of these characters.
和
If you must embed the content, the following patterns can be used:
<style>/*<![CDATA[*/ ... /*]]>*/</style>
这需要付出很大的努力,而切换到 HTML 是一个更简单的选择。浏览器是为 HTML 设计的,其 XHTML 支持是事后才提供的。如果您从 XHTML 中获得任何好处,那就是在发布过程中将其与服务器端工具一起使用……而这样做的人并不多。
关于javascript - 错误: Not allowed element when it is in JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15350077/