首页 > 代码库 > Javascript中parentNode的用法

Javascript中parentNode的用法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script type="text/javascript">
        window.onload = function () {
            //取出所有的a标签
            var oUl = document.getElementById("ul1");
            var aA = oUl.getElementsByTagName("a");
            for (var i = 0; i < aA.length; i++) {
                //给每个a标签注册事件
                aA[i].onclick = function () {
                    //this是当前元素,parentNode  是单数,因为每个节点,只有一个父节点!有多个子节点
                   this.parentNode.style.display = "none";
                }
            }
        }
    </script>
</head>
<body>
    -----------点击“隐藏” 隐藏每行中的文字-----------
    <ul id="ul1">
        <li>电风扇发<a href=http://www.mamicode.com/"javascript:void(0)">隐藏</a></li>
        <li>电松岛枫扇发<a href=http://www.mamicode.com/"javascript:void(0)">隐藏</a></li>
        <li>大幅度<a href=http://www.mamicode.com/"javascript:void(0)">隐藏</a></li>
        <li>合格后<a href=http://www.mamicode.com/"javascript:void(0)">隐藏</a></li>
        <li>的发的发的我<a href=http://www.mamicode.com/"javascript:void(0)">隐藏</a></li>
    </ul>
</body>
</html>