Html 为什么我们需要禁用=“禁用”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6531738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:20:27  来源:igfitidea点击:

Why do we need disabled="disabled"?

htmlwebxhtml

提问by NorvayHais

It seems to be common consensus that for XHTML attributes which do not require any value, we should repeat the attribute name. E.g. <input disabled>in correct XHTML is <input disabled="disabled"/>.

对于不需要任何值的 XHTML 属性,我们应该重复属性名称,这似乎是一个普遍的共识。例如<input disabled>在正确的 XHTML 中是<input disabled="disabled"/>.

However, we can get the HTML <input>element to be disabled using anyof the following:

但是,我们可以<input>使用以下任何一种方法禁用HTML元素:

  • <input disabled=" "/>

  • <input disabled=""/>

  • <input disabled="asdfg">

  • <input disabled="false">

  • <input disabled=" "/>

  • <input disabled=""/>

  • <input disabled="asdfg">

  • <input disabled="false">

Is there actually an official rule to use disabled="disabled"? Or is it a matter of taste?

实际上有官方规则可以使用disabled="disabled"吗?还是口味问题?

回答by Spudley

The officially correct xhtmlsyntax is disabled="disabled".

官方正确的xhtml语法是disabled="disabled".

The reason for this is that xhtml is an XML syntax, and XML requires that attributes have values. The xhtml specs also explicitly specify that the value should be "disabled".

这样做的原因是 xhtml 是一种 XML 语法,而 XML 要求属性具有值。xhtml 规范还明确指定该值应为“禁用”。

The reason for the choice of this value over any other possible value was fairly arbitrary; they simply decided that all previously boolean attributes should be converted to XML format by making their value the same as their name.

选择这个值而不是任何其他可能的值的原因是相当武断的;他们只是决定将所有以前的布尔属性转换为 XML 格式,使它们的值与其名称相同。

So yes, there is an official spec which says you must use that full syntax. But it only applies to xhtml documents. You can find it here(if you search for disabledin that page, you will find that it is listed as only allowing "disabled"as the value. Similarly for the readonlyand checkedattributes).

所以是的,有一个官方规范说你必须使用完整的语法。但它只适用于 xhtml 文档。您可以在此处找到它(如果您disabled在该页面中搜索,您会发现它被列为仅允许"disabled"作为值。对于readonlychecked属性也是如此)。

Plain HTML - both v4 and v5 - isn't tied to XML's restrictions in this way, and doesn't require an attribute value for disabled; the mere existence of the disabledattribute is sufficient to disable the field, regardless of whether you have a value for the attribute, or what that value is.

纯 HTML - v4 和 v5 - 以这种方式不受 XML 的限制,并且不需要disabled;的属性值。仅仅存在disabled属性就足以禁用该字段,无论您是否具有该属性的值,或者该值是什么。

The final upshot of all this is that if you are using an XHTML doctype, or you wish to remain XML-compliant, you should use disabled="disabled". If you're not using XHTML and you don't care about having valid XML syntax, then you can just use disabledon its own, or with any attribute value you like.

所有这一切的最终结果是,如果您使用的是 XHTML 文档类型,或者您希望保持 XML 兼容,您应该使用disabled="disabled". 如果您不使用 XHTML 并且不关心是否具有有效的 XML 语法,那么您可以单独使用disabled,或者使用您喜欢的任何属性值。

One other thing I would note (getting slightly off topic, but may be relevant) is that this may have an impact on any CSS or JQuery code that may reference the field. For example, I've seen people using JQuery selectors such as $('[disabled=disabled]'), and similar in CSS. This obviously relies on the attribute having the expected value. Therefore, if you're going to reference a boolean attribute like this in a selector, you should reference it without a value, like so: $('[disabled]')as this will work whatever the attribute is set to.

我要注意的另一件事(稍微偏离主题,但可能是相关的)是这可能会对可能引用该字段的任何 CSS 或 JQuery 代码产生影响。例如,我看到有人$('[disabled=disabled]')在 CSS 中使用 JQuery 选择器,例如, 和类似的。这显然依赖于具有预期值的属性。因此,如果您要在选择器中引用这样的布尔属性,则应该在没有值的情况下引用它,就像这样:$('[disabled]')因为无论属性设置为什么,这都将起作用。

回答by Fenton

There is documentation for this, the official term for these kind of attributes is "Boolean Attributes"

有这方面的文档,这些属性的官方术语是“布尔属性”

The official standard is on the W3C website http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2

官方标准在 W3C 网站http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2

It says that:

它说:

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

它们出现在元素的开始标记中意味着该属性的值为“true”。他们的缺席意味着“假”的价值。

For your XHTML mark-up to be valid, you must use the long-hand

为了使您的 XHTML 标记有效,您必须使用长手

<option selected="selected">

For HTML, you can use the minimised form

对于 HTML,您可以使用最小化形式

<option selected>

回答by Alohci

@Spudley has already given you the link to the XHTML specs to provide the official documentation.

@Spudley 已经为您提供了 XHTML 规范的链接以提供官方文档。

The choice of making both the attribute and the value the same in XHTML was not entirely arbitrary though.

在 XHTML 中使属性和值都相同的选择并不是完全随意的。

As @Sohnee says, XHTML 1.0 was a reformulation of HTML4.01 in XML and the goal was to stick to HTML 4 patterns as much as possible to make the transition as easy as possible for web authors.

正如@Sohnee 所说,XHT​​ML 1.0 是在 XML 中对 HTML4.01 的重新表述,其目标是尽可能地坚持 HTML 4 模式,以使网络作者尽可能轻松地进行转换。

HTML has always supported disabled="disabled"as a valid form for boolean attributes, and the reason for that is it permitted the shortened attribute disabledto be defined in SGML. (Note @Thaddee Tyl's "bit odd" comment)

HTML 一直支持disabled="disabled"作为布尔属性的有效形式,原因是它允许disabled在 SGML 中定义缩短的属性。(注意@Thaddee Tyl 的“有点奇怪”评论)

In SGML, the disabledon it's own attribute isn't the attribute name without a value, it's the attribute value without a name. i.e. the name is inferred from the value. To make all this work in SGML and be backward compatible with what browsers have always done, the name and the value have to be defined to be the same.

在SGML 中,disabled其自身的属性不是没有值的属性名,而是没有名称的属性值。即名称是从值中推断出来的。为了使所有这些在 SGML 中工作并与浏览器一直在做的事情向后兼容,必须将名称和值定义为相同。

Note that this only affects SGML based validation. Browsers' parser logic isn't SGML based and has never cared for this subtlety, hence you can, in practice put in whatever value for the attribute you like.

请注意,这仅影响基于 SGML 的验证。浏览器的解析器逻辑不是基于 SGML 的,并且从不关心这种微妙之处,因此您可以在实践中为您喜欢的属性输入任何值。

HTML5 validation isn't SGML based either, so the restriction has been relaxed. disabled=""is now valid. disabled="true"and disabled="false"are not valid though, because disabled="false"is confusing, since as you note, it disables, not enables the control. See http://www.w3.org/TR/html5/common-microsyntaxes.html#boolean-attributesfor details.

HTML5 验证也不基于 SGML,因此限制已放宽。disabled=""现在有效。disabled="true"并且disabled="false"无效,因为disabled="false"令人困惑,因为正如您所注意到的,它禁用而不是启用控件。有关详细信息,请参阅http://www.w3.org/TR/html5/common-microsyntaxes.html#boolean-attributes

回答by Thaddee Tyl

An html4 documentspecifies this:

一个 html4 文档指定了这个:

Boolean attributes may legally take a single value: the name of the attribute itself.

布尔属性可以合法地采用单个值:属性本身的名称。

Boolean attributes were always a bit odd in the sgml world, so really you can put anything you want. Browser implementation is all that matters.

布尔属性在 sgml 世界中总是有点奇怪,所以你真的可以放任何你想要的东西。浏览器实现才是最重要的。