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

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

问题描述

对于不需要任何价值的XHTML属性,似乎普遍认为我们应该重复属性名称。例如。 < input disabled> 在正确的XHTML中是< input disabled =disabled/>



然而,我们可以使用任何的元素来禁用HTML < input> 元素如下:


  • < input disabled =/>


  • < input disabled =/>


  • < input disabled =asdfg>


  • < input disabled =false>




实际上有一个官方规则使用 disabled =disabled?或者它是一个品味问题? 正确的 xhtml 语法是 disabled =disabled



原因是xhtml是XML语法,XML要求属性具有值。 xhtml规格还明确指定该值应该被禁用。



选择此值超过任何其他可能值的原因相当随意;他们简单地认为所有以前的布尔属性都应该通过将它们的值与它们的名称相同来转换为XML格式。

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



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



所有这些的最终结果是,如果您使用的是XHTML文档类型,或者您希望保持XML兼容,则应该使用 disabled =disabled code>。如果您没有使用XHTML,而且您也不在意使用有效的XML语法,那么您可以单独使用 disabled ,或者使用任何您喜欢的属性值。

我会注意到的另一件事(略微偏离主题,但可能相关)是,这可能会影响可能引用该字段的任何CSS或JQuery代码。例如,我见过使用JQuery选择器的人,比如 $('[disabled = disabled]'),在CSS中类似。这显然依赖于具有期望值的属性。因此,如果你要在选择器中引用一个像这样的布尔属性,你应该引用它没有值,如下所示: $('[disabled]')因为无论属性设置为何,这都可以工作。


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

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

  • <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?

解决方案

The officially correct xhtml syntax is 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".

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.

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

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.

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天全站免登陆