HTML4中允许没有值的属性吗? [英] are attributes without value allowed in HTML4?

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

问题描述

我想知道HTML 4是否允许没有值的属性,等同于带有空值的属性.例如:

<h2 section>foobar</h2>

代替:<h2 section="">foobar</h2>

两个摘要是否同样有效?如果不是,它们在HTML版本5中有效吗?

谢谢!

解决方案

布尔属性,是的,它们是完全有效的.

来自W3C: (在SGML和HTML上)

某些属性起着布尔变量的作用(例如,选定的 OPTION元素的属性).它们在开始标签中的出现 元素的表示该属性的值为"true".他们的 如果不存在,则表示值为"false".

布尔属性可以合法地采用一个值: 属性本身(例如selected="selected").

这表明布尔属性在HTML4中也是有效的,但是如果使用类似的属性,则将是无效的.因为该布尔值属于option标记. .stackoverflow.com/transcript/message/14732353#14732353> @ Ronni Skansing 澄清了疑问.

<p selected>Hello</p>


HTML5文档:

来自W3C :

空属性语法

某些属性可以通过仅提供属性来指定 名称,没有价值.


来自W3C: (HTML 5.1 Nightly)

许多属性是布尔属性.一个的存在 元素上的boolean属性表示真实值,并且 没有该属性表示错误值.


但是

section 无效属性,如果要定义自己的属性,HTML5提供了一种方法来实现..您需要使用 data- 前缀,例如,您的 section 应该写为data-section,这样您的属性将被计为有效.


如果您愿意的话,我们总是有一个验证器来检查- W3C标记验证服务

^验证为HTML5


注意:尽管我提供了data-适用于HTML5,但使用自定义 HTML4中的属性无效,即使您定义了data- 属性名称之前,但布尔属性在HTML4中有效 也是

I wonder if HTML 4 allows attributes without value, as being equivalent to attributes with an empty value. For example:

<h2 section>foobar</h2>

instead of:<h2 section="">foobar</h2>

Are the two snippets equally valid? If not, are they valid in HTML version 5?

thanks!

解决方案

Boolean Attributes, Yes they are completely valid.

From W3C: (On SGML & HTML)

Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".

Boolean attributes may legally take a single value: the name of the attribute itself (e.g., selected="selected").

This states that Boolean attributes are valid in HTML4 as well, but if you use something like, would be invalid.. because that boolean belongs to option tag.. Thanks to @Ronni Skansing for clarifying the doubt..

<p selected>Hello</p>


HTML5 Docs :

From W3C :

Empty Attribute Syntax

Certain attributes may be specified by providing just the attribute name, with no value.


From W3C: (HTML 5.1 Nightly )

A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.


BUT

section is an invalid attribute, if you want to define your own attributes, HTML5 provides a way to do that.. you need to use data- prefix, for example, your section should be written as data-section, this way your attribute will be counted as valid.


If you hesitate to do so, we always have a validator to check - W3C Markup Validation Service

^ Validated As HTML5


NOTE: Though I provided data- is applicable for HTML5, using custom attributes in HTML4 is invalid, no matter even if you define data- before the attribute name, but, boolean attributes are valid in HTML4 as well.

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

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