JavaScript中的Attribute和Property区别和联系

  • property属于DOM,是JavaScript的对象;attribute是html的自带属性,可以通过attributes属性或者getAttribute/setAttribute方法进行访问
  • property的属性值可以是任意类型,attribute的属性值类型只能是string
  • property包含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>

 

完整说明

posted @ 2022-04-21 12:08  我是格鲁特  阅读(64)  评论(0编辑  收藏  举报