Html 你引用 HTML5 属性吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6495310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:17:18  来源:igfitidea点击:

Do you quote HTML5 attributes?

htmlperformancestandards

提问by ryanve

Attribute quotes are optionalin HTML5.

属性引号在 HTML5 中是可选的。

What are the pros/cons to quoting them?

引用它们的利弊是什么?

id=example                     <!--quotes optional-->
href=http://example.com        <!--quotes optional-->
class="example example-1"      <!--quotes required due to space-->
href="http://example.com/p=47" <!--quotes required due to '=' sign-->

Update: Added advantages based on the answers:

更新:根据答案增加了优势:

Advantages to quoting all attributes:

引用所有属性的优点:

  • all editors can deal with it properly
  • more consistent
  • better portability (easier to change doctype)
  • easier to maintain (esp. if attributes might become empty)
  • easier to 'find and replace' changes
  • cleaner doc (if you think quotes improve readability)
  • ?
  • 所有编辑都可以妥善处理
  • 更一致
  • 更好的可移植性(更容易更改文档类型)
  • 更容易维护(特别是如果属性可能变空)
  • 更容易“查找和替换”更改
  • 更干净的文档(如果您认为引号可以提高可读性)
  • ?

Advantages to omittingoptional quotes:

省略可选引号的优点:

  • slightly reduced filesize
  • cleaner doc (if you prefer minimal text)
  • ?
  • 文件大小略有减少
  • 更干净的文档(如果您喜欢最少的文字)
  • ?

回答by Pekka

I'm in favour of always using quotes.

我赞成总是使用引号。

  • It looks waycleaner and more consistent

  • All editors can deal with it properly

  • It's easier to maintain - you can edit values without breaking them because quotes are missing.

  • 它看起来方式更清洁和更一致的

  • 所有编辑都能妥善处理

  • 它更容易维护 - 您可以编辑值而不会因为缺少引号而破坏它们。

The few bytes you save in document size by dropping quotes where they are not needed are not worth mentioning (unless maybe you're Google's home page).

通过在不需要的地方删除引号来节省文档大小的几个字节是不值得一提的(除非您可能是 Google 的主页)。

回答by Courtney Christensen

I do quote all attributes and will continue to do so. Primarily because it visually distinguishing where the attribute value starts and stops.

我确实引用了所有属性,并将继续这样做。主要是因为它在视觉上区分了属性值的开始和停止位置。

Additionally, it just makes sense for portability and compatibility reason. Though the quotes are optional in HTML[5], they are notoptional in XHTML. You have a lot of tedious work to do if you need to convert your documents to XHTML (say, to display SVG on Webkit browsers). We reallydon't need to dredge up the XHTML v. HTML debate, but it seems like too little hassle to not quote your attributes.

此外,出于可移植性和兼容性的原因,它才有意义。尽管引号在 HTML[5] 中是可选的,但它们在 XHTML中不是可选的。如果您需要将文档转换为 XHTML(例如,在 Webkit 浏览器上显示 SVG),您将有很多繁琐的工作要做。我们真的不需要深入讨论 XHTML 与 HTML 的争论,但是不引用您的属性似乎太麻烦了。

Saving a few bytes in the document body is nigh insignificant when you're downloading kilobytes and kilobytes of images and JavaScript libraries.

当您下载千字节和千字节的图像和 JavaScript 库时,在文档正文中保存几个字节几乎无关紧要。

回答by Jamie Dixon

I think one of the advantages to quoting all attributes is consistency.

我认为引用所有属性的优点之一是一致性。

If you're quoting some (with spaces or certain symbols), it feels nicer to me to quote all of them.

如果你引用了一些(带有空格或某些符号),我觉得引用所有这些感觉更好。

回答by shanethehat

I would say that because quotes are required in some instances you should use them consistently. This makes your code cleaner and easier to read. Switching between just creates unnecessary confusion.

我会说,因为在某些情况下需要引号,所以您应该始终如一地使用它们。这使您的代码更清晰,更易于阅读。在两者之间切换只会造成不必要的混乱。

回答by shanethehat

HTML5 was designed with backward compatibility in mind, so quoting attributes is okay. Many folks argue that since XHTML requires quotes it can't hurt to always include them. Personally I choose to quote. Required? No.

HTML5 的设计考虑了向后兼容性,因此引用属性是可以的。许多人争辩说,由于 XHTML 需要引号,因此始终包含它们不会有什么坏处。我个人选择引用。必需的?不。

Here's some guidance I find reasonable. http://www.impressivewebs.com/html5-syntax-style/

这里有一些我认为合理的指导。http://www.impressivewebs.com/html5-syntax-style/