不带引号的HTML属性? [英] HTML properties without quotation marks?

查看:144
本文介绍了不带引号的HTML属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



< div class =service_definition>

我一直认为html在他的属性中需要引号。 c $ c>或< div class ='service_definition'>



但是最近我注意到w3验证程序不会将以下内容识别为错误:

< div class = service_definition>



如果我省略了引号,那么它是否正确?或者是否有任何限制?

解决方案

总是使用引号。 b

我不认为不带引号的HTML属性会归类为无效HTML,但它们可能会在后期导致您的问题。


默认情况下,SGML要求使用
或双引号(ASCII小数点34)或单引号
标记(ASCII小数点39)来分隔所有属性值。当值由双引号
分隔时,单引号可以包含在
属性值内,反之亦然。作者还可以使用数字字符引用
表示双引号()和单引号(')。对于双
引号,作者也可以使用字符实体引用。



在某些情况下,作者可以指定不带任何引号的属性
的值,属性值可能只包含
个字母(az和AZ),数字(0- 9),连字符(ASCII小数点45),
句点(ASCII小数点46),下划线(ASCII小数点95)和冒号
(ASCII小数点58)我们建议使用引号,即使它是
可能消除它们。

来源: http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.2






我认为它们是明确定义属性值何时开始和结束的好方法。 / p>

类为例

 < 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 = and id =123 code>



(即使是stackoverflow的代码着色系统也在为此付出努力!)


I've always thought html requires quotation marks in his properties:

<div class="service_definition"> or <div class='service_definition'>

But recently i noticed that the w3 validator doesn't recognize the following as an error:

<div class=service_definition>

So is it all right if i omit the quotation marks? Or are there any restrictions?

解决方案

Always use quotation marks.

I don't believe that HTML properties without quotation marks are classed as Invalid HTML, but they will potentially cause you problems later on down the line.

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.

Source: http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.2


I think they're a great way of clearly defining when an attribute value starts and finishes.

Take for example the class attribute which can have multiple classes seperated by spaces:

<div class="classa classb" id="123">

This clearly shows the browser that my classes are classa and classb, with an element id of 123.

Take away the quotation marks and we've got:

<div class=classa classb id=123>

A browser could now interpret this as 3 classes, with no id. classa, classb and id=123.

Or it may even interpret it as 3 attributes. class="classa", classb="" and id="123"

(Even stackoverflow's code colouring system is struggling with this one!)

这篇关于不带引号的HTML属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆