js设置,获取,删除属性(setAttribute, getAttribute, removeAttribute)

设置,获取,删除属性

  • setAttribute()

setAttribute() 方法添加指定的属性,并为其赋指定的值。

如果这个指定的属性已存在,则仅设置/更改值。

  • getAttribute()

getAttribute() 方法返回指定属性名的属性值。

  • removeAttribute()

removeAttribute() 方法删除指定的属性。

<body>
    <div>
        <input value="3652">
        <input type="button" onClick="setClick()" value="点击设置">
        <input type="button" onClick="getClick()" value="点击查询">
        <input type="button" onClick="deleteClick()" value="点击删除">
    </div>
</body>
</html>
<script>
    var input = document.getElementsByTagName("input")[0];

    // setAttribute 设置input的type为file
    function setClick(){
        input.setAttribute("type","file")
    }

    // getAttribute 输出input的type
    function getClick(){
        console.log(input.getAttribute("type"));
    }

    //removeAttribute 删除input的value值
    function deleteClick(){
        input.removeAttribute("value")
    }
</script>

 


版权声明:本文为lily__an原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。