javascript学习笔记03

DOM讲解:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>js01_hello</title>
    <meta name="author" content="Administrator" />
    <script type="text/javascript">
        function getPwd() {
            //DOM中的第一个常用的方法是getElementById-->表示通过id来获取某个特定的标签,获取的是一个值
            var pwd = document.getElementById("pwd");
            // alert(pwd.value);
            var pc = document.getElementById("showPwd");
            pc.innerHTML = pwd.value;
        }
        
        function getUsers() {
            //根据标签的name属性来获取一组标签对象,这个方法一般都只用于表单的获取
            var users = document.getElementsByName("users");
            for(var i=0;i<users.length;i++) {
                alert(users[i].value);
            }
        }
        
        function getInputs() {
            //根据标签的名称获取一组元素,这个非常的有用,一般用于获取html的各种标签,以此完成各种操作
            //一共能够获取页面的10个元素
            var is = document.getElementsByTagName("input");
            for(var i=0;i<is.length;i++) {
                alert(is[i].value);
            }
        }
    </script>
</head>
<body>
    <input type="text" name="users" />
    <input type="text" name="users" />
    <input type="text" name="users" />
    <input type="text" name="users" />
    <input type="text" name="users" />
    <input type="text" name="users" />
    <br/>
    <input type="password" id="pwd" />
    <br/>
    <input type="button" value="获取users" onclick="getUsers()"/>
    <input type="button" value="获取PWD" onclick="getPwd()"/>
    <input type="button" value="获取Input" onclick="getInputs()"/>
    <br/>
    <div id="showPwd">
        <h1>dddd</h1>
    </div>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>js01_hello</title>
    <meta name="author" content="Administrator" />
    <script type="text/javascript">
    function getAllH1() {
        var ah = document.getElementsByTagName("h1");
        for(var i=0;i<ah.length;i++) {
             //获取节点中的文本内容
             alert(ah[i].innerHTML);
             //获取节点的名称
             alert(ah[i].nodeName);
             //获取节点的类型
             alert(ah[i].nodeType);
             //获取节点中的值:节点中的值只是在针对文本节点时有用
             alert(ah[i].nodeValue);
             //获取某个节点的文本节点
             alert(ah[i].firstChild.nodeType);
            获取某个文本节点的值,对于IE和firefox而言文本的空格不一致,对于IE而言,仅仅只会把换行加入空白,但是FF而言就是全部空格
            所以在获取文本节点值的时候,需要把空格去除
            alert("|"+ah[i].firstChild.nodeValue+"|");
        }
    }
    
    function getConH2() {
        var con = document.getElementById("content");
        var h2 = con.getElementsByTagName("h2");
        //得到的h2元素是一个数组
        alert(h2[0].innerHTML);
        //通过h2这个节点来找到h3中span的值
        //1、找到父节点
        var pn = h2[0].parentNode;
        //2、通过父节点找到名称为h3的节点
        var h3 = pn.getElementsByTagName("h3")[0];
        //3、通过h3找到span
        var s = h3.getElementsByTagName("span")[0];
        alert(s.innerHTML);
    }
    </script>
</head>
<body>
    <div id="content">
        <h1>
            aaaaa1
            <span>aaaaassss</span>
        </h1>
        
        <h2>
            bbbbbbbbb1
            <span>bbbbbssss</span>
        </h2>
        
        <h3>
            cccccccc1
            <span>ccccccssss</span>
        </h3>
    </div>
    <h1>
        hhhhhhhaaaaa1
        <span>hhhhhhhhhhhaaaaassss</span>
    </h1>
    
    <h2>
        hhhhhhhhhhbbbbbbbbb1
        <span>hhhhhhbbbbbssss</span>
    </h2>
    
    <h3>
        hhhhhhhhhhhcccccccc1
        <span>hhhhhhhhhhhhhccccccssss</span>
    </h3>
    
    <input type="button" value="获取所有的h1" onclick="getAllH1()" />
    <input type="button" value="获取content的h2" onclick="getConH2()" />
</body>
</html>

原文地址:https://www.cnblogs.com/canceler/p/4515876.html