为什么我们需要禁用=“禁用"? [英] Why do we need disabled="disabled"?

查看:24
本文介绍了为什么我们需要禁用=“禁用"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是,我们可以使用以下任何来禁用 HTML 元素:

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

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

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

推荐答案

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

The officially correct xhtml syntax is disabled="disabled".

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

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

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

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.

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

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 disabled in that page, you will find that it is listed as only allowing "disabled" as the value. Similarly for the readonly and checked attributes).

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

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 disabled attribute is sufficient to disable the field, regardless of whether you have a value for the attribute, or what that value is.

这一切的最终结果是,如果您使用 XHTML 文档类型,或者您希望保持 XML 兼容,您应该使用 disabled="disabled".如果您不使用 XHTML 并且不关心是否具有有效的 XML 语法,那么您可以单独使用 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 disabled on its own, or with any attribute value you like.

我要注意的另一件事(稍微偏离主题,但可能是相关的)是这可能会对可能引用该字段的任何 CSS 或 JQuery 代码产生影响.例如,我看到有人使用 JQuery 选择器,例如 $('[disabled=disabled]'),以及类似的 CSS.这显然依赖于具有预期值的属性.因此,如果您要在选择器中引用这样的布尔属性,则应该在没有值的情况下引用它,如下所示: $('[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.

这篇关于为什么我们需要禁用=“禁用"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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