quoting HTML attribute values

自古美人都是妖i 提交于 2019-12-12 01:34:44

问题


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.


回答1:


Both are fine, but Double quotes are better (IMHO) as you reduce the risk of dynamic values causing errors. e.g.

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

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

vs.

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

<input value="O'Graddy"/>



回答2:


Either is good, as long as you use it. " is more popular.




回答3:


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



来源:https://stackoverflow.com/questions/2887714/quoting-html-attribute-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!