简体   繁体   English

引用HTML属性值

[英]quoting HTML attribute values

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. 但是我认为“是更干净的方法,也许只是我在C和C ++的语法下长大了。

What is the cleanest way of quoting attribute values and why? 引用属性值的最干净方法是什么?为什么? Please no subjective answers. 请没有主观答案。

Both are fine, but Double quotes are better (IMHO) as you reduce the risk of dynamic values causing errors. 两者都很好,但是双引号 (IMHO)更好,因为可以减少动态值导致错误的风险。 eg 例如

<input value='${lastName}'/>

<input value='O'Graddy'/>
                ^^^^^^^

vs.

<input value="${lastName}"/>

<input value="O'Graddy"/>

Either is good, as long as you use it. 只要您使用它,任何一种都很好。 " is more popular. "更受欢迎。

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 如果您有兴趣,我不久前对HTML,CSS和JavaScript中的未引用属性值进行了一些研究,并在此处进行了介绍: 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 我还创建了一个工具,该工具将告诉您输入的值是否为有效的未引用属性值: http : //mothereffingunquotedattributes.com/#foo%7Cbar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM