不带引号的 HTML 属性?

标签 html

我一直认为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">

这清楚地向浏览器显示我的类是 classaclassb,元素 ID 为 123

去掉引号,我们得到:

<div class=classa classb id=123>

浏览器现在可以将其解释为 3 个类,没有 id。 classaclassbid=123

或者它甚至可以将其解释为 3 个属性。 class="classa"classb=""id="123"

(即使是 stackoverflow 的语法样式也很难解决这个问题!)

关于不带引号的 HTML 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9837063/

相关文章:

java - 如何使用 Selenium 访问此特定元素? (包括页面源)

html - 制作带边框的登录表单和模糊的背景

javascript - 带有两个提交按钮的 Angular 模态在 "enter"上触发

wordpress - 你在 HTML 中使用 <hr> 吗?

html - iPad (iOS 8) 上的水平悬停菜单导致错误元素中的点击/单击事件

html - 伪元素在元素的顶部而不是在元素的底部

html - 无法让 flex 行出现在新行的全宽上

Jenkins 职位描述中的 HTML

javascript - 隐藏带有复选框的 div

android - 如何停止 Android 中 WebView 的内部滚动?