JavaScript中的Attribute和Property区别和联系
property
属于DOM,是JavaScript的对象;attribute
是html的自带属性,可以通过attributes属性或者getAttribute/setAttribute方法进行访问property
的属性值可以是任意类型,attribute
的属性值类型只能是stringproperty
包含attribute
中html自带的属性,不包含自定义属性- attribute获取html属性值时,只能获取到初始值,无法获取到变化后的值
<input id="input" value="123" /> <script> var dom = document.getElementById('input'); console.log(dom.getAttribute('value')) //123; dom.onchange = function(){ console.log(dom.getAttribute('value')) //不管在输入框中输入的是啥值,获取的还是初始值 } </script>