इस disabledपर विशेषता सेट करने का आधिकारिक तरीका HTMLInputElementयह है:
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');
जबकि @ कौशर का उत्तर सक्षम और अक्षम करने के लिए पर्याप्त है HTMLInputElement, और शायद IE के ऐतिहासिक रूप से छोटी गाड़ी के कारण क्रॉस-ब्राउज़र संगतता के लिए बेहतर है setAttribute, यह केवल इसलिए काम करता है क्योंकि Elementगुण छाया Elementविशेषताएँ। यदि कोई संपत्ति सेट की जाती है, तो DOM बराबर विशेषता के मूल्य के बजाय डिफ़ॉल्ट रूप से संपत्ति के मूल्य का उपयोग करता है।
गुणों और विशेषताओं के बीच बहुत महत्वपूर्ण अंतर है। एक सच्ची HTMLInputElement संपत्ति का एक उदाहरण है input.value, और नीचे दिखाया गया है कि कैसे काम करता है:
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);
}
मुझे उम्मीद है कि यह गुणों और विशेषताओं के बीच किसी भी भ्रम को स्पष्ट करता है।