zoukankan      html  css  js  c++  java
  • JavaScript 查找元素 查找兄弟元素 previousSibling nextSibling

    查找兄弟元素 previousSibling nextSibling

    示例

    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
    32
    33
    <!DOCTYPE html>
    <html>
        <head>
            <title>访问元素</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style>
                .mycolor{
                    color:#fff;
                    
                }
                .steam{
                    color: red;
                }
            </style>
        </head>
        <body>
            <ul id="ul">
                <li id="ali">油条</li><li id="bli">包子</li><li id="cli">米饺</li><li id="dli"><a id="yufen">鱼粉</a></li>
            </ul>
            <script>
                // 查找父节点
                var tempel = document.getElementById("bli");
                tempel.className="steam";
                 
                var previousNode = tempel.previousSibling;
                previousNode.className="mycolor";
     
                var nextNode = tempel.nextSibling;
                nextNode.className="mycolor";
            </script>
        </body>
    </html>

    兄弟元素查找比较坑,只是能元素在同一行时才能查询

    查询子节点 firstChild lastChild

    示例

    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
    32
    33
    <!DOCTYPE html>
    <html>
        <head>
            <title>访问元素</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <style>
                .mycolor{
                    color:#fff;
                    
                }
                .steam{
                    color: red;
                }
            </style>
        </head>
        <body>
            <ul id="ul"
                ><li id="ali">油条</li
                ><li id="bli">包子</li
                ><li id="cli"><a id="yufen">米饺</a></li
                ><li id="dli">鱼粉</li
            ></ul>
            <script>
                // 查找子节点
                var tempel = document.getElementById("ul");          
                var firstNode = tempel.firstChild;           
                firstNode.className = "mycolor";
                var lastNode = tempel.lastChild;
                lastNode.className = "mycolor";
            </script>
        </body>
    </html>

    注意,<ul></ul>这段代码,要写成这样才正常查询到。

  • 相关阅读:
    git的优秀教程
    线性表的顺序存储结构和链式存储结构的比较
    python3基础知识学习记录
    thinkPHP为什么设置一个单入口文件?
    2017年读过的专业书
    DFS(深度优先搜索)模板
    HDOJ2553-N皇后问题(DFS)
    POJ(2784)Buy or Build
    并查集 分类: 并查集 2015-07-09 16:32 0人阅读 评论(0) 收藏
    Number of Containers(数学) 分类: 数学 2015-07-07 23:42 1人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/lsyw/p/11168116.html
Copyright © 2011-2022 走看看