问题
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