问题描述
在 JavaScript 代码周围使用 HTML 注释标签是否仍然相关?
Is it still relevant to use HTML comment tag around JavaScript code?
我是说
<html>
<body>
<script type="text/javascript">
//<!--
document.write("Hello World!");
//-->
</script>
</body>
</html>
推荐答案
HTML 注释,即.,不再需要.它们旨在允许不理解
标签的浏览器优雅地降级.这些浏览器,例如.Netscape 1.x 不再存在于野外.所以再把 HTML 注释放在你的脚本标签中真的没有意义了.
HTML comments, ie. <!-- -->
, are no longer needed. They were intended to allow browsers that didn't understand the <script>
tag to degrade gracefully. These browsers, eg. Netscape 1.x are no longer found in the wild. So there is really no point in putting HTML comments in your script tags anymore.
如果您希望 HTML 验证为 XHTML 或 XML,您可能需要使用注释掉的 CDATA 标记.
If you want your HTML to validate as XHTML or XML, you probably want to use a commented out CDATA tag.
<script type="text/javascript">
//<![CDATA[
document.write("Hello World!");
//]]>
</script>
这样做的原因是你的 <
、>
、&
、"
和 ' 不必编码为 <
、>
、&
, "
和 '
分别.
The reason for this is so your <
, >
, &
, "
and '
that are part of your javascript code won't have to be encoded as <
, >
, &
, "
and '
respectively.
这篇关于使用 HTML 注释标签 <!-- -->仍然与 JavaScript 代码相关吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!