element对象

1.getAttribute获取属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>element对象</title>
</head>
<body>
    <!-- 
     getAttribute属性名
     -->
    <input type="text" id="inputid" value="加油啊,进度有点慢呢">
    <script type="text/javascript">
    var input1 = document.getElementById("inputid");
    //getAttribute得到属性里面的值
    alert(input1.getAttribute("value"));
    alert(input1.getAttribute("id"));
    </script>
</body>
</html>


2.setAttribute设置属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>element对象</title>
</head>
<body>
    <!-- 
     getAttribute属性名
     -->
    <input type="text" id="inputid" value="加油啊,进度有点慢呢">
    <script type="text/javascript">
    var input1 = document.getElementById("inputid");
    //设置属性值
    alert(input1.getAttribute("class"));
    input1.setAttribute("class","果子一定要加油");
    alert(input1.getAttribute("class"));
    </script>
</body>
</html>


3.removeAttribute删除属性

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>element对象</title>
</head>
<body>
    <!-- 
     getAttribute属性名
     -->
    <input type="text" name="果子请你拼命,不然你会一事无成" id="inputid" value="加油啊,进度有点慢呢">
    <script type="text/javascript">
    var input1 = document.getElementById("inputid");
    //removeAttribute删除属性
    alert(input1.getAttribute("name"));
    input1.removeAttribute("name");
    alert(input1.getAttribute("name"));
    </script>
</body>
</html>

注意:removeAttribute不能删除value属性

猜你喜欢

转载自www.cnblogs.com/zjm1999/p/10428597.html