JavaScript DOM操作案例移除元素的自定义属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        div {
            width: 200px;
            height: 200px;
            background-color: pink;
        }
        .cls {
            background-color: deeppink;
        }
    </style>
</head>
<body>
<input type="button" value="移除属性" id="bt"/>
<div score = "10" id="dv" class="cls"></div>
<script src="common.js"></script>
<script>
    //点击按钮移除元素的自定义属性
    my$("bt").onclick = function () {
        my$("dv").removeAttribute("score");
        //移除元素的类样式
        //值没有了但是属性还有
        my$("dv").className = "";
        //也可以移除元素自带的属性
        my$("dv").removeAttribute("class");
    };
</script>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/cuilichao/p/9368740.html