【发布时间】:2011-03-02 04:11:58
【问题描述】:
我了解到您可以通过将disable 附加到其标签而不是作为属性来禁用(使物理上无法点击)HTML 按钮,如下所示:
<input type="button" name=myButton value="disable" disabled>
由于此设置不是属性,我如何通过 JavaScript 动态添加此设置以禁用之前启用的按钮?
【问题讨论】:
标签: javascript html
我了解到您可以通过将disable 附加到其标签而不是作为属性来禁用(使物理上无法点击)HTML 按钮,如下所示:
<input type="button" name=myButton value="disable" disabled>
由于此设置不是属性,我如何通过 JavaScript 动态添加此设置以禁用之前启用的按钮?
【问题讨论】:
标签: javascript html
由于此设置不是属性
这是一个属性。
某些属性被定义为布尔值,这意味着您可以指定它们的值而忽略其他所有内容。即,您只包括粗体部分,而不是 disabled="disabled"。在 HTML 4 中,您应该只包含粗体部分,因为完整版本被标记为 a feature with limited support(尽管现在这在编写规范时不太正确)。
从 HTML 5 开始,规则发生了变化,现在您只包含名称而不包含值。这没有实际区别,因为名称和值是相同的。
DOM property 也称为 disabled,是一个布尔值,采用 true 或 false。
foo.disabled = true;
理论上你也可以foo.setAttribute('disabled', 'disabled'); 和foo.removeAttribute("disabled"),但我不相信旧版本的Internet Explorer 会这样(众所周知,setAttribute 存在漏洞)。
【讨论】:
foo 中的 foo.disabled = true; 是什么?是那个按钮的id吗?
禁用
document.getElementById("btnPlaceOrder").disabled = true;
启用
document.getElementById("btnPlaceOrder").disabled = false;
【讨论】:
$('#btnPlaceOrder').disabled = false; 没有。
$('#btnPlaceOrder')[0].disabled = false 作为 jquery 选择器似乎返回一个数组。耸耸肩。
它是一个属性,但是一个布尔值(所以它不需要名称,只需要一个值——我知道,这很奇怪)。您可以在 Javascript 中设置等效的属性:
document.getElementsByName("myButton")[0].disabled = true;
【讨论】:
尝试以下方法:
document.getElementById("id").setAttribute("disabled", "disabled");
【讨论】:
在HTMLInputElement 上设置disabled 属性的官方方法是这样的:
var input = document.querySelector('[name="myButton"]');
// Without querySelector API
// var input = document.getElementsByName('myButton').item(0);
// disable
input.setAttribute('disabled', true);
// enable
input.removeAttribute('disabled');
虽然@kaushar's answer 足以启用和禁用HTMLInputElement,并且由于IE 历史上存在错误setAttribute,它可能更适合跨浏览器兼容性,但它仅适用于Element 属性遮蔽Element 属性。如果设置了属性,则 DOM 默认使用该属性的值,而不是等效属性的值。
属性和属性之间有一个非常重要的区别。 input.value 是真正的HTMLInputElement 属性 示例,下面演示了阴影的工作原理:
var input = document.querySelector('#test');
// the attribute works as expected
console.log('old attribute:', input.getAttribute('value'));
// the property is equal to the attribute when the property is not explicitly set
console.log('old property:', input.value);
// change the input's value property
input.value = "My New Value";
// the attribute remains there because it still exists in the DOM markup
console.log('new attribute:', input.getAttribute('value'));
// but the property is equal to the set value due to the shadowing effect
console.log('new property:', input.value);
<input id="test" type="text" value="Hello World" />
这就是说属性影子属性的意思。这个概念也适用于prototype 链上的继承属性:
function Parent() {
this.property = 'ParentInstance';
}
Parent.prototype.property = 'ParentPrototype';
// ES5 inheritance
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
function Child() {
// ES5 super()
Parent.call(this);
this.property = 'ChildInstance';
}
Child.prototype.property = 'ChildPrototype';
logChain('new Parent()');
log('-------------------------------');
logChain('Object.create(Parent.prototype)');
log('-----------');
logChain('new Child()');
log('------------------------------');
logChain('Object.create(Child.prototype)');
// below is for demonstration purposes
// don't ever actually use document.write(), eval(), or access __proto__
function log(value) {
document.write(`<pre>${value}</pre>`);
}
function logChain(code) {
log(code);
var object = eval(code);
do {
log(`${object.constructor.name} ${object instanceof object.constructor ? 'instance' : 'prototype'} property: ${JSON.stringify(object.property)}`);
object = object.__proto__;
} while (object !== null);
}
我希望这可以澄清关于属性和属性之间区别的任何混淆。
【讨论】:
它仍然是一个属性。将其设置为:
<input type="button" name=myButton value="disable" disabled="disabled">
...有效。
【讨论】:
如果你有按钮对象,叫做 b:b.disabled=false;
【讨论】:
我认为最好的方法可能是:
$("#ctl00_ContentPlaceHolder1_btnPlaceOrder").attr('disabled', true);
跨浏览器运行良好。
【讨论】:
prop,而不是attr。
<button disabled=true>text here</button>
您仍然可以使用属性。只需使用“禁用”属性而不是“值”。
【讨论】:
disabled="disabled" 或只是 disabled。任何字符串值都等价于"disabled",包括disabled="true"和disabled="false"。