为元素编写自关闭标签不是传统上空的坏习惯吗? [英] Is writing self closing tags for elements not traditionally empty bad practice?

查看:21
本文介绍了为元素编写自关闭标签不是传统上空的坏习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到 jQuery(或者是 Firefox)会改变我的一些 <span class="presentational"></span>进入<span class="presentational"/>

I have noticed jQuery (or is it Firefox) will turn some of my <span class="presentational"></span> into <span class="presentational" />

现在我的问题是,这样写我的标记可以吗?任何浏览器都会卡住它吗?

Now my question is, is this okay to write my markup like this? Will any browsers choke on it?

就我个人而言,如果 <span class="presentational"/> 为空,我认为它看起来更简洁.

Personally, I think it looks cleaner to do <span class="presentational" /> if it's going to be empty.

推荐答案

我假设您的问题与在 Firefox 中查看源代码时自关闭元素上的红色尾部斜杠有关.如果是这样,那么您已经陷入了浏览器制造商与 Web 开发者之战中最激烈但同时也是被动的激进辩论之一.XHTML 不仅仅是关于文档的标记.它还涉及如何通过网络提供文档.

I'm assuming your question has to do with the red trailing slash on self-closing elements when you view source in Firefox. If so, you've stumbled into one of the most vehement, yet simultaneously passive aggressive debates in the browser maker vs. web developer wars. XHTML is NOT just about a document's markup. It's also about how documents are meant to be served over the web.

在我开始之前;我正在努力不偏袒任何一方.

Before I begin; I'm trying hard not to take sides here.

XHTML 1.1 规范规定,Web 服务器应提供内容类型为 application/xhtml+xml 的 XHTML.Firefox 将那些尾部斜杠挑出无效,因为您的文档是作为 text/html 而不是 application/xhtml+xml 提供的.以这两个例子为例;相同的标记,一个作为 application/xhtml+xml,另一个作为 text/html.

The XHTML 1.1 spec says that a web server should serve XHTML with a Content-Type of application/xhtml+xml. Firefox is singling out those trailing slashes as invalid because your document is being served as text/html rather than application/xhtml+xml. Take these two examples; identical markup, one served as application/xhtml+xml, the other as text/html.

http://alanstorm.com/testbed/xhtml-as-html.php

http://alanstorm.com/testbed/xhtml-as-xhtml.php

Firefox 将元标记中的尾部斜杠标记为对使用 text/html 提供的文档无效,而对使用 application/xhtml+xml 提供的文档有效.

Firefox flags the trailing slash in the meta tag as invalid for the document served with text/html, and valid for the document served with application/xhtml+xml.

对于浏览器开发人员来说,XHT​​ML 的重点是您可以将您的文档视为 XML,这意味着如果有人向您发送了无效的内容,规范说您不必解析它.所以,如果一个文档作为 application/xhtml+xml 并且有格式不正确的内容,开发人员可以说不是我的问题".你可以在这里看到它的作用

To a browser developer, the point of XHTML is you can treat your document as XML, which means if someone sends you something that's not valid, the spec says you don't have to parse it. So, if a document is served as application/xhtml+xml and has non-well formed content, the developer is allowed to say "not my problem". You can see that in action here

http://alanstorm.com/testbed/xhtml-not-valid.php

当文档以 text/html 形式提供时,Firefox 会将其视为一个普通的旧 HTML 文档并使用宽容、为您修复它、解析例程

When a document is served as text/html, Firefox treats it as a plain old HTML document and uses the forgiving, fix it for you, parsing routines

http://alanstorm.com/testbed/xhtml-not-valid-as-html.php

因此,对于浏览器制造商来说,XHT​​ML 作为 text/html 是荒谬的,因为浏览器的渲染引擎从未将其视为 XML.

So, to a browser maker, XHTML served as text/html is ludicrous, because it's never treated as XML by the browser's rendering engine.

几年前,希望不仅仅是标签猴子的 Web 开发人员(免责声明:我将自己包括在内)开始寻找开发最佳实践的方法,这些方法不涉及三次嵌套表,但仍然允许引人注目的设计体验.他们/我们选择了 XHTML/CSS,因为 W3C 说这是未来,唯一的选择是单一供应商(微软)控制事实上的标记规范的世界.真正的邪恶在于单一供应商,而不是微软.我发誓.

A bunch of years ago, web developers looking to be more than tag monkeys (Disclaimer: I include myself as one of them) started looking for ways to develop best practices that didn't involved thrice nested tables, but still allowed a compelling design experience. They/We latched onto XHTML/CSS, because the W3C said this was the future, and the only other choice was a world where a single vendor (Microsoft) controlled the defacto markup spec. The real evil there being the single vendor, and not so much Microsoft. I swear.

那么争议在哪里?application/xhtml+xml 有两个问题.第一个是 Internet Explorer.IE 中有一个遗留错误/功能,其中作为 application/xhtml+xml 的内容将提示用户下载文档.如果您尝试使用 IE 访问上面列出的 xhtml-as-xhtml.php,那很可能会发生这种情况.这意味着如果你想使用 application/xhtml+xml,你必须浏览器嗅探 IE,检查 Accepts 头并且只向那些接受它的浏览器提供 application/xhtml+xml.这并非微不足道 听起来是对的,但也违背了 Web 开发人员所追求的一次编写"原则.

So where's the controversy? There are two problems with application/xhtml+xml. The first is Internet Explorer. There's a legacy bug/feature in IE where content served as application/xhtml+xml will prompt the user to download the document. If you tried to visit the xhtml-as-xhtml.php listed above with IE that's likely what happened. This means if you want to use application/xhtml+xml, you have to browser sniff for IE, check the Accepts header and only serve application/xhtml+xml to those browsers that accept it. This is not as trivial as it sounds to get right, and also went against the "write once" principle that the web developers were striving for.

第二个问题是 XML 的严酷性.这又是一个容易引发问题的问题,但有些人认为单个错误标签或未正确编码的单个字符不应导致用户看不到他们想要的文档.换句话说,是的,规范说如果 XML 格式不正确,你应该停止处理它,但用户并不关心规范,他们关心他们的猫的网站被破坏了.

The second problem is the harshness of XML. This is, again, one of those flame prone issues, but there's some people who think a single bad tag, or single character improperly encoded shouldn't result in a user not seeing the document they want to. In other words, yes, the spec says you should stop processing XML if it's not well formed, but the user doesn't care about the spec, they care that their cat's website is broken.

XHTML 1.0(而不是 1.1)规范说,XHT​​ML 文档可以作为 text/html 提供给这个问题,假设某些兼容性指南.诸如 img 标签会自动关闭之类的东西.这里的关键词是可能.在 RFC 发言 中,可能表示可选.Firefox 已选择不将使用 XHTML doctype 提供的文档处理为 XHTML,而是将 text/html 内容类型处理为 XHTML.但是,W3C 验证器很乐意将这些文档报告为有效.

Adding even more gasoline to the issue is the XHTML 1.0 (not 1.1) spec says that XHTML documents may be served as text/html, assuming certain compatibility guidelines are followed. Things like the img tag being self closing and the like. The key word here is may. In RFC speak, may means optional. Firefox has chosen NOT to treat documents served with an XHTML doctype but a content type of text/html as XHTML. However, the W3C validator will happily report these documents as valid.

我会让读者思考一种文化同时存在的奇迹/恐惧,这种文化会编写文档来定义可能这个词的含义.

I'll leave the reader to ponder the simultaneous wonder/horror of a culture that writes a document to define what they mean by the word may.

最后,这就是 HTML 5 的全部内容.XHTML 成为政治上的烫手山芋,以至于一群想要推动语言发展的人决定转向另一个方向.他们为 HTML 5 制定了规范.目前正在 W3C 中进行讨论,预计将在未来十年的某个时候完成.与此同时,浏览器供应商正在从正在进行的规范中挑选和实施这些功能.

Finally, this is what the whole HTML 5 thing is about. XHTML became such a political hot potato that a bunch of people who wanted to move the language forward decided to go in another direction. They produced a spec for HTML 5. This is currently being hashed out in the W3C, and expected to finish sometime in the next decade. In the meantime, browser vendors are picking and choosing features from the in-progress spec and implementing them.

在评论中,Alex 指出,如果您要嗅探某物,您应该选中 Accept头以查看 application/xhtml+xml 是否被用户代理接受.

In the comments, Alex points out that if you're going to sniff for something, you should check the Accept header to see if application/xhtml+xml is accepted by the user agent.

这是完全正确的.一般来说,如果您要嗅探,请嗅探功能,而不是浏览器.

This is absolutely correct. In general, if you're going to sniff, sniff for the feature, not for the browser.

这篇关于为元素编写自关闭标签不是传统上空的坏习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆