JS修改元素属性

<script>
    var tu = document.getElementById("img1");   // 通过ID获取元素
    console.log(tu);
    tu.className = "c1";    // 设置类
    tu.src = "2.jpg";       // 设置属性
    tu.style.border = "1px solid red";  // 设置属性


    // 获取属性值
    console.log(tu.getAttribute("src"));    // 2.jpg
    // 设置属性,如果已存在此属性则修改,如果没有则添加
    tu.setAttribute("src","3.jpg");
    // 移除属性
    tu.removeAttribute("src");

</script>
Last modification:June 26th, 2019 at 04:46 pm
If you think my article is useful to you, please feel free to appreciate

Leave a Comment