为什么 Dreamweaver 在注释中添加 CSS 代码?我们是否应该始终使用此注释来在中使用 CSS?

发布于 2024-08-16 20:09:44 字数 280 浏览 9 评论 0原文

在注释中添加CSS代码有什么好处?

<STYLE type="text/css">
<!--
   H1 { color: red }
   P  { color: blue}
   -->
</STYLE>

我们应该一直使用它还是没有必要?它与 JavaScript 的 CDATA 相同吗?

What is the benefit to add CSS code in comments?

<STYLE type="text/css">
<!--
   H1 { color: red }
   P  { color: blue}
   -->
</STYLE>

Should we use this always or isn't there any need? Is it same like CDATA for JavaScript?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹雪碎 2024-08-23 20:09:44

它是 15 多年前就支持 HTML 2.0 浏览器的古老产物。这些浏览器不知道

如今这种做法绝对没有任何意义,但由于剪切粘贴编码和 Dreamweaver 等糟糕的创作工具,它仍然存在。

在用作 XML 的 XHTML 中,您不得包含注释标记,否则您的脚本或样式表实际上只是注释,将被完全忽略。

一些作者对 XHTML 中的脚本和样式表使用 部分,但这是出于完全不同的原因。它允许您在内容中包含文字 <& 字符,而无需执行正确的 XML 操作并将其转义为 << /code> 或 &。除了使代码更难阅读之外,脚本和样式中的 HTML 转义还会混淆传统的非 XML HTML 解析器:在 HTML4 中,脚本和样式元素很神奇,可以在不转义的情况下包含这些字符(您可以使用的一个序列) t 使用 因为它结束了元素)。

当您在打算由非 XML HTML 解析器读取的文档中使用 CDATA 部分时,您应该使用 CSS 或 JavaScript 注释来隐藏 CDATA 部分标记:

<script type="text/javascript">//<![CDATA[
    ...
//]]></script>

<style type="text/css">/* <![CDATA[ */
    ...
/* ]]> */</style>

对于 HTML 3.2 中常见的老式注释包装, CSS 和 JS 解析有一个例外,允许 序列在不被注意的情况下溜过,但 ]]> 这将被 CSS 或 JavaScript 解析器视为语法错误。

如果(但愿不会)您需要支持 HTML 2.0、3.2+ 和 XHTML 解析器,那么您最终会需要这种可怕的混乱:

<script type="text/javascript"><!--//--><![CDATA[//><!--
    ...
//--><!]]></script>

<style type="text/css"><!--/*--><![CDATA[/*><!--*/
    ...
/*]]>*/--></style>

幸运的是这永远不会发生。

您通常可以完全不用注释或 CDATA ,因为 CSS 几乎从不需要使用 <& 字符,并且在 JavaScript 中您通常可以重新转换小于比较是无害的 >,逻辑与是反转逻辑或。当您想要将 <& 字符放入字符串文字(例如 '\x3C')。

无论如何,大量的样式和脚本(您最有可能想要使用这些字符的地方)通常应该位于外部链接文件中,这使得这一点毫无意义。

It's archaic cruft meant to support HTML 2.0 browsers from 15-odd years ago. Those browsers didn't know about the <script> and <style> tags, so unless the content inside them was hidden inside comment markup, it would spit them out onto the page as text content, causing a nasty mess.

There is absolutely no purpose to the practice today, but still it hangs on, thanks to cut-and-paste coding and poor authoring tools like Dreamweaver.

In XHTML served as XML you must not include the comment markup, otherwise your script or stylesheet really is only a comment and will be completely ignored.

Some authors use a <![CDATA[ ... ]]> section for scripts and stylesheets in XHTML, but this is for a completely different reason. It allows you to include a literal < or & character inside the content without having to do the correct XML thing and escape it to < or &. Apart from making code harder to read, an HTML-escape in scripts and styles would confuse legacy non-XML HTML parsers: in HTML4, the script and style elements are magic and can have those characters in without escaping (the one sequence you can't use is </ since that ends the element).

When you do use a CDATA section in a document you intend to be read by non-XML HTML parsers, you should use a CSS or JavaScript comment to hide the CDATA section markup:

<script type="text/javascript">//<![CDATA[
    ...
//]]></script>

<style type="text/css">/* <![CDATA[ */
    ...
/* ]]> */</style>

For the old-school comment-wrapping common in HTML 3.2, an exception was made to CSS and JS parsing to allow <!-- and --> sequences to slip through unnoticed, but this is not the case for <![CDATA[ or ]]> which would be seen by a CSS or JavaScript parser as a syntax error.

If, heaven forbid, you needed to support HTML 2.0, 3.2+ and XHTML parsers, you'd end up needing this hideous mess:

<script type="text/javascript"><!--//--><![CDATA[//><!--
    ...
//--><!]]></script>

<style type="text/css"><!--/*--><![CDATA[/*><!--*/
    ...
/*]]>*/--></style>

thankfully this is never going to happen.

You can usually get away with no comment or CDATA cruft at all, since CSS almost never needs to use the < or & characters, and in JavaScript you can usually recast less-than comparisons as harmless >, and logical-ands to inverted logical-ors. Both CSS and JavaScript offer native escapes for when you want to put a < or & character in a string literal (eg. '\x3C').

In any case, significant amounts of style and script — where you're most likely to want to use those characters — should generally be in an external linked file, making the point moot.

烈酒灼喉 2024-08-23 20:09:44

这样做是为了向后兼容不理解样式标签的渲染引擎。通过将样式放在注释中,引擎(希望)不会将它们呈现为纯文本。

由于大多数客户端都可以正确渲染样式,这种做法不再那么重要,但仍然很常见。我通常在自己的代码中忽略它,但这是个人喜好。

This is done for backwards compatibility with rendering engines that do not understand the style tag. By placing your styles in a comment, the engine will (hopefully) not render them as plain text.

This practice is less important now that the majority of clients can render styles properly, but it is still somewhat common to see. I generally neglect it in my own code, but that's personal preference.

櫻之舞 2024-08-23 20:09:44

您应该尽可能将您的样式包含在外部样式表中。当您必须将它们包含在页面上时,看到包含在 块中的内容比注释更常见。

这些注释不会在任何现代浏览器中禁用规则。

You should always include your styles in external stylesheets whenever possible. When you have to include them on the page, it more common to see the contents wrapped in <!CDATA[[ ... ]]> blocks than comments.

The comments will not disable the rules in any modern browsers.

叹倦 2024-08-23 20:09:44

在 Dreamweaver (DW) 中,更改页面的 doctype 声明非常“方便”。

如果使用XHTML

无论如何,它(几乎)与其他文档类型声明向后兼容,DW 始终使用此约定是有意义的。

In Dreamweaver (DW), it's very 'convenient' to change the page's doctype declaration.

If XHTML is used, <!-- .... --> or<!CDATA[[ ... ]]> will kick in to enable strict parsing and compliance.

In any case, it is backward compatible (almost) with other doctype declarations, it make sense for DW to always use this convention.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文