我一直认为html需要在他的属性中加引号:
<div class="service_definition">
或 <div class='service_definition'>
但最近我注意到 w3 验证器没有将以下内容识别为错误:
<div class=service_definition>
那我省略引号可以吗?或者有什么限制吗?
最佳答案
始终使用引号。
我不认为没有引号的 HTML 属性被归类为无效 HTML,但它们可能会在以后给您带来问题。
By default, SGML requires that all attribute values be delimited using either double quotation marks (ASCII decimal 34) or single quotation marks (ASCII decimal 39). Single quote marks can be included within the attribute value when the value is delimited by double quote marks, and vice versa. Authors may also use numeric character references to represent double quotes (") and single quotes ('). For double quotes authors can also use the character entity reference ".
In certain cases, authors may specify the value of an attribute without any quotation marks. The attribute value may only contain letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58). We recommend using quotation marks even when it is possible to eliminate them.
来源:http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.2
我认为它们是明确定义属性值何时开始和结束的好方法。
以 class
属性为例,它可以有多个用空格分隔的类:
<div class="classa classb" id="123">
这清楚地向浏览器显示我的类是 classa
和 classb
,元素 ID 为 123
。
去掉引号,我们得到:
<div class=classa classb id=123>
浏览器现在可以将其解释为 3 个类,没有 id。 classa
、classb
和 id=123
。
或者它甚至可以将其解释为 3 个属性。 class="classa"
、classb=""
和 id="123"
(即使是 stackoverflow 的语法样式也很难解决这个问题!)
关于不带引号的 HTML 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9837063/