没有引号的HTML属性? [英] HTML attributes without quotation marks?

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

问题描述

我一直认为html在其属性中需要用引号引起来:

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

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

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

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?

推荐答案

始终使用引号.

我不认为不带引号的HTML属性被归类为无效HTML,但是它们可能会在以后给您带来麻烦.

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.

默认情况下,SGML要求使用双引号(ASCII十进制34)或单引号标记(ASCII十进制39).单引号可以包含在其中当属性值用双引号分隔时,反之亦然.作者还可以使用数字字符引用来代表双引号()和单引号(').对于双引号引号作者还可以使用字符实体引用."

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 ".

在某些情况下,作者可以指定属性的值没有任何引号.该属性值只能包含字母(a-z和A-Z),数字(0-9),连字符(ASCII十进制45),句点(ASCII十进制46),下划线(ASCII十进制95)和冒号(ASCII十进制58).我们建议即使在使用引号的情况下也要使用引号可以消除它们.

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

我认为它们是清晰定义属性值何时开始和结束的好方法.

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

class 属性为例,该属性可以具有多个由空格分隔的类:

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

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

这清楚地显示了浏览器,我的类是 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>

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

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

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

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

(即使stackoverflow的语法样式也为此感到困扰!)

(Even stackoverflow's syntax styling is struggling with this one!)

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

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