本文介绍了HTML 正文中的 style 和 script 标签......为什么不呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[这与这个问题有关,但不是因为它与电子邮件无关.]

[This is related to this question, but not since it's not about email.]

在许多情况下——尤其是在使用 CMS 或其他人的框架时,嵌入 标签和 标签要容易得多.这似乎适用于 IE6、IE7 (Windows)、Firefox 3.x 和 Safari (OS X).

In many cases -- especially when working with a CMS or someone else's framework, it's much easier to embed <style> tags and <script> tags in the <body> than in the <head>. This seems to work in IE6, IE7 (Windows), Firefox 3.x and Safari (OS X).

严格来说,这是错误的吗?如果是,除了在某些客户中被完全忽略之外,它可能会导致什么负面后果?

Strictly speaking, is this wrong? And if it is, what negative consequences might it cause... aside from being completely ignored in some clients?

注意:很高兴每个人都想谈论 DRY 和集中式风格.想象一下,我想在文档中使用样式标签,因为它们不是全局的,而且我无法在每页的基础上访问头部.无论出于何种原因,网站在逐页或每段的基础上都不同.我对难以追踪和改变不感兴趣.我担心在正文中使用样式可能带来的后果.

Note: Glad everybody wants to talk about DRY and centralizing styles. Imagine for a second that I want to use style tags within a document because they ARE NOT GLOBAL and that I DO NOT HAVE ACCESS TO THE HEAD ON A PER-PAGE BASIS. For whatever reason, be it that the site differs on a page-by-page basis, or a per-paragraph basis or whatever. I'm not interested in it being hard to track down and change. I'm worried about possible consequences of using style in the body.

您将重要的东西集中起来.中央样式表中的其他所有内容都变得臃肿.

You centralize stuff that's central. Everything else is bloat in the central stylesheets.

推荐答案

尽管规范明确说明 body 标签中不允许使用样式标签,但规范并不是最重要的.每个主要浏览器都在正文中支持样式标签,这最终是用户查看您网站的方式.* 虽然长期以来浏览器行业一直在推动更好的标准和标准支持,但长期以来也一直普遍推动呈现损坏文档也可以.

Although the specs explicitly state style tags are not permitted in the body tag, specs aren't all that matters. Style tags are supported in the body by every major browser, and that's ultimately how users see your site.* While there has long been a drive for better standards and standards support in the browser industry, there's also long been a general push to render broken documents as well as can be.

领导 HTML5 规范工作的 Google 同时维护 google.com,该网站违反规范以节省字节,方法是将引号排除在其属性值之外,使用针对 CSS 规范的选择器技巧,包括没有类型或语言的脚本标签, 和没有类型的链接标签.纯粹主义者可能会争辩说,互联网上最常用的网站之一违反了规范,并且存在被严重误译的严重危险.或者,我们可以推断没有浏览器会进入流行的使用范围,无法在规范中呈现如此广泛使用的黑客.

Google, who leads the HTML5 spec effort, simultaneously maintains google.com which violates specs to save bytes, by leaving the quotes out of its attribute values, using selector hacks against the CSS spec, including script tags with no type or language, and link tags with no type. A purist could argue one of the most used sites on the internet is violating the specs and in serious danger of being horribly misrendered. Or, we can reason that no browser will enter popular use that can't render such widely used hacks on the spec.

因此,问题更多的是浏览器行业的发展方向 - 这又是两个更好的规范之一,但也尽最大努力尊重违反这些规范的页面的意图.我敢打赌,样式标签会在很长一段时间内继续在正文中发挥作用.

So, the question is more of which way the browser industry is going - which again is one of both better specs, but also doing their best to honor the intent of pages that violate those specs. My bet is style tags will keep working in the body for a long time to come.

*在撰写本文时,Firefox 3+、IE6+、Safari 2+、Chrome 12+ 中的 HTML5 文档类型支持正文中的样式标签.支持可能可以追溯到更远的地方,但这些浏览器在互联网上很少见.

*As of this writing, style tags in the body are supported with an HTML5 doctype in Firefox 3+, IE6+, Safari 2+, Chrome 12+. Support likely goes back farther but those browsers are rarely seen on the interwebs.

这篇关于HTML 正文中的 style 和 script 标签......为什么不呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:20