自定义属性的操作(设置、获取、移除)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        window.onload = function () {
            var box = document.getElementById('box');
            // 自有的属性
            console.log(box.id);

            // 自定义属性通过setAttribut实现
            box.setAttribute('age', 10);

            // 获取自定义属性
            console.log(box.getAttribute('age'));

            // 移除自定义属性
            box.removeAttribute('age');
        }
    </script>
</head>
<body>
<div class="text" id="box">你好</div>

</body>
</html>
原文地址:https://www.cnblogs.com/ella-li/p/14544380.html