其中一个比另一个更可取吗?为什么?性能如何——如果这些被调用了数千次?
A) element.setAttribute("disabled", true);
B) element.disabled = true;
它们似乎都禁用了 FF 4 中的 input[text] 元素。
最佳答案
一般来说……
使用属性。很长一段时间(直到版本 7 或 8 IIRC),Internet Explorer 的 setAttribute
实现严重中断,它会设置属性而不是属性(经典的失败点是 class
因为没有 class
属性(它是 className
)。
特别是在这种情况下...... element.setAttribute("disabled", true);
是错误的。应该是 element.setAttribute("disabled", "disabled");
关于javascript - 通过 property 或 setAttribute 设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8018814/