在javascript中,差异input.value = val与input.setAttribute('value',val)?

时间:2011-07-23 10:27:10

标签: javascript dom attributes properties

在javascript中,将HTMLElement属性设置为赋值与使用setAttribute()之间的区别是什么。以下内容来自chrome会话,让我相信的一个区别:

> i = document.createElement('input');
<input>
> i.value = 'abc';
"abc"
> i
<input>​
> i.setAttribute('value','abc');
undefined
> i
<input value=​"abc">

究竟有什么区别?这是你在屁股中字节的东西吗?


直接回答。

chrome显示属性,因此导致我的困惑。

1 个答案:

答案 0 :(得分:7)

  

在javascript中,将HTMLElement属性设置为赋值与使用setAttribute()

之间的区别是什么

这取决于财产。

value属性反映当前值,value属性反映默认值。

某些属性直接映射到属性。

相关问题