我知道规范允许 ' 和 " 作为分隔符属性值,而且我也知道始终引用是一个很好的做法。
不过我认为“这是更干净的方式,也许这只是我在 C 和 C++ 语法的陪伴下长大的。”
引用属性值的最简洁方式是什么?为什么?请不要主观回答。
I know the spec allows both ' and " as delimiters for attribute values, and I also know it's a good practice to always quote.
However I consider " being the cleaner way, maybe it's just me having grown up with C and C++' syntax.
What is the cleanest way of quoting attribute values and why? Please no subjective answers.
发布评论
评论(3)
两者都很好,但双引号更好(恕我直言),因为您可以降低动态值导致错误的风险。例如
与
Both are fine, but Double quotes are better (IMHO) as you reduce the risk of dynamic values causing errors. e.g.
vs.
如果您想省略属性值周围的引号,则需要记住很多规则。一致地使用引号可能是最简单的;它避免了各种问题。
如果您有兴趣,我不久前对 HTML、CSS 和 JavaScript 中不带引号的属性值进行了一些研究,并在这里写了相关内容:http://mathiasbynens.be/notes/unquoted-attribute-values
我还创建了一个工具,可以告诉您输入的值是否是有效的不带引号的属性值:http://mothereffingunquotedattributes.com/#foo%7Cbar
There’s a lot of rules to remember if you want to omit quotes around attribute values. It’s probably easiest to just use quotes consistently; it avoids all kinds of problems.
If you’re interested, I did some research on unquoted attribute values in HTML, CSS and JavaScript a while ago, and wrote about it here: http://mathiasbynens.be/notes/unquoted-attribute-values
I’ve also created a tool that will tell you if a value you enter is a valid unquoted attribute value or not: http://mothereffingunquotedattributes.com/#foo%7Cbar
哪个都好,只要你会用。
"
更受欢迎。Either is good, as long as you use it.
"
is more popular.