为什么将属性“禁用”但不是“启用” [英] Why attribute "disabled" but not "enabled"

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

问题描述

我总是感觉到HTML < input> disabled 属性,所有这些都扭曲了我的大脑。为什么选择否定属性名称,是不是启用更直观?

只需比较:

 启用=高级用户
禁用=未(高级用户)

启用=未(锁定)
禁用=锁定

启用=高级用户而不是(锁定)
禁用=不是(高级用户)或锁定
禁用=不是(高级用户和不(锁定))


解决方案

背后的根本原因在于它是HTML输入字段,并且需要这种方式来维持与现有网页的向后兼容性。



< input> 标签最初被定义,其功能非常有限。它没有禁用 readonly 属性,也没有许多其他我们认为理所当然的属性。



这些都是后来添加的,但是当它们被添加时,许多网站已经使用< input> 字段,所以禁用它的能力必须在不影响没有使用它的现有代码的情况下工作。因此,默认状态必须是启用



它也必须是布尔标志,这就是为什么它是禁用而不是 enabled = true 。后者将是一个键值对属性。这不是一个好的选择。



考虑以下几点:

  enabled = false 
enabled = 0
enabled = FALSE
enabled = no
enabled = disabled
enabled = flase
etc ...

浏览器必须能够处理大量可能的值。使它成为一个布尔标志极大地简化了事情。这使得该规范更容易理解,无论是网站开发人员还是浏览器开发人员。



另外要记住的是添加此属性的时间到HTML是在所谓的浏览器战争中。许多功能被添加到竞争的网络浏览器,匆忙,没有正式规格的好处,并添加了许多功能,我们可以真正回顾并希望它稍有不同。



我不认为这是这些功能之一:如果您停下来思考它, disabled 标志完全符合逻辑。但是,如果浏览器开发者在当时合作得多一点,它可能会更好地设计出来。



但是无论如何,今天的情况是这就是我们所拥有的。 HTML规范可能会不断发展,但现有的功能现在不会改变。


I'm always feel the disabled attribute for HTML <input> and all is twisting my brain. Why choose a negated attribute name, isn't enabled more intuitive?

Just compare:

enabled=advancedUser
disabled=not(advancedUser)

enabled=not(locked)
disabled=locked

enabled=advancedUser and not(locked)
disabled=not(advancedUser) or locked
disabled=not(advancedUser and not(locked))

解决方案

The fundamental reason behind this is that it was a later addition to the HTML input fields, and needed to be that way to maintain backward compatibility with existing web pages.

When the <input> tag was originally defined, its functionality was extremely limited. It did not have disabled or readonly attributes, nor many of the other properties we take for granted today.

These were all added later, but by the time they were added, many web sites were already using <input> fields, so the ability to disable it had to work without affecting existing code that didn't use it. Therefore the default state had to be enabled.

It also had to be a boolean flag, which is why it is disabled rather than enabled=true. The latter would have been a key-value pair attribute. This wouldn't have been a good choice.

Consider the following:

enabled=false
enabled=0
enabled=FALSE
enabled=no
enabled=disabled
enabled=flase
etc...

The browser would have had to be able to cope with a huge number of possible values. Making it a boolean flag simplifies things enormously. It makes the spec easier to understand, both for the web site developer and the browser developer.

The other thing to bear in mind is that the time when this property was added to HTML was in the middle of the so-called 'browser wars'. Many features were being added to the competing web browsers, in a hurry and without the benefit of formal specs, and many features were added which we can indeed look back on and wish it were slightly different.

I don't believe this is one of those features: the disabled flag is perfectly logical really if you stop and think about it. But it's quite possible that it may have been better designed if the browser developers had been co-operating a bit more back then.

But whatever the case, the situation today is that this is what we have. The HTML spec may be evolving, but existing features such as this are not going to change now.

这篇关于为什么将属性“禁用”但不是“启用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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