DOM中的DOM被禁用属性 [英] DOM 'disabled' property in javascript

查看:117
本文介绍了DOM中的DOM被禁用属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到如何使用禁用属性来禁用javascript中的表单元素的确定答案。

I can't find a definitive answer to how to use the disabled property to disable form elements in javascript.

有些地方说这是一个简单的布尔值。其他人的说法是将其设置为字符串值disabled以禁用,将空字符串''启用。或者添加和删除已禁用的属性,通常与字符串值相结合。

Some places say it's a simple boolean. Other's say to set it to the string value 'disabled' to disable, empty string '' to enable. Or there's adding and removing the disabled attribute, usually in combination with a string value.

那么通过浏览器执行哪种方法呢? (没有jQuery)。
如何像启用标志一样简单的东西如此被屠杀?

So which is the proper way to do it cross browser? (no jQuery). How did something as simple as an enable flag get so butchered?

- 编辑 -

对不起,我应该提到我正在为IE8开发(不是选择)。

Sorry, I should have mentioned I'm developing for IE8 (not by choice).

推荐答案

以下输入元素全部禁用:

The following input elements are all disabled:

<input disabled />
<input disabled="" />
<input disabled="disabled" />
<input disabled="true" />
<input disabled="false" /> <!-- still disabled! -->

如果一个布尔属性存在, ;否则,它是关闭的。该值并不意味着任何东西。

If a boolean attribute is present, it's on; otherwise, it's off. The value doesn't mean anything.

但是,当通过javascript处理这些元素时,您应该使用相应的属性,即:

However, when dealing with these elements through javascript, you should make use of the corresponding property, i.e.:

myInput.disabled = true; // adds the attribute and disables control
myInput.disabled = false; // removes the attribute and enables control

该属性将为您更新属性。所有布尔属性/属性对都是如此,即: readonly checked selected 等。

The property will update the attribute for you. This is true of all boolean attribute / property pairs, i.e.: readonly, checked, selected, etc.

混淆可能来自于 setAttribute() 请求名称和值,并在 key =value格式 - 即使你不想要一个值。添加自定义布尔属性时,只需设置属性(示例):

Confusion may stem from the fact that setAttribute() asks for both a name and value and emits markup in a key="value" format -even when you don't want a value. When adding a custom boolean attribute, I simply set the attribute without a value (sample):

input.setAttributeNode(document.createAttribute("data-custom"));
console.log(input); // <input data-custom>

请参阅文档。 createAttribute() 元素< a href =https://developer.mozilla.org/en-US/docs/Web/API/element.setAttributeNode =nofollow> setAttributeNode()

这篇关于DOM中的DOM被禁用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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