为什么我的脚本无法通过 w3c 验证? [英] Why is my script failing w3c validation?
问题描述
我有这段代码:
<script language="javascript" type="text/jscript">
document.write("<img src='http://dm.leadgenesys.com/jpgp.lgt?en=P.........TP_Q=&ur=' + escape(document.referrer) + '' border='0' alt='no alt' />");
</script>
并且...当我尝试验证它时,出现此错误:
and... when I try to validate it, I'm given this error:
文档类型不允许元素img"在这里
document type does not allow element "img" here
…rer) + '" border="0" alt="no alt"/>');
…rer) + '" border="0" alt="no alt" />');
上面命名的元素是在一个不允许的上下文.这个可能意味着你错误地嵌套元素——例如样式"代替body"部分中的元素内部头"——或两个元素重叠(这是不允许的).
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).
知道我可以做些什么来使这个 JavaScript w3c 兼容吗?
Any idea what I can do to make this JavaScript w3c compliant?
推荐答案
简单:不要尝试将 JavaScript 验证为 HTML.
Simple: don't try to validate your JavaScript as HTML.
您可以通过多种方式执行此操作...但目前最好的方法是将其移至单独的 JS 文件中,然后从一个简短的脚本中调用它
You can do this in a number of ways... But the best by far is to move it out into a separate JS file, and then either call it from a short script
<body>
...
<script type="text/javascript" language="javascript">WriteImage();</script>
...
</body>
...或者更好的是,完全放弃 document.write()
并在 DOM 加载后对其进行操作.
...or better yet, ditch document.write()
entirely and manipulate the DOM after it has loaded.
这篇关于为什么我的脚本无法通过 w3c 验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!