HTML4中是否允许没有值的属性?

前端之家收集整理的这篇文章主要介绍了HTML4中是否允许没有值的属性?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想知道 HTML 4是否允许没有值的属性,因为它等同于具有空值的属性.例如:
<h2 section>foobar</h2>

而不是:< h2 section =“”> foobar< / h2>

这两个片段同样有效吗?如果没有,它们是否在HTML版本5中有效?

谢谢!

解决方法

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

From W3C:(在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").

这表明布尔属性在HTML4中也是有效的,但是如果你使用类似的东西,则会无效..因为布尔值属于选项标签..感谢@Ronni Skansing澄清疑问..

<p selected>Hello</p>

HTML5文档:

From W3C

属性语法

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

From W3C:(每晚HTML 5.1)

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.

section是一个无效的属性,如果你想定义自己的属性,HTML5提供了一种方法来做到这一点..你需要使用data-前缀,例如,你的节应该写成数据节,这样你的属性将是算作有效.

如果您犹豫不决,我们总会有一个验证员来检查 – W3C Markup Validation Service

^验证为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.

猜你在找的HTML相关文章