JavaScript 查找元素


查詢單個元素document.getElementById

示例

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
<!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;
                
            }
        </style>
    </head>
    <body>
        <ul id="ul">
            <li id="a">油條</li>
            <li id="b">包子</li>
            <li id="c">米餃</li>
            <li id="d"><a>魚粉</a></li>
        </ul>
        <script>
            // 返回單一元素節點
            var el = document.getElementById('a');
            el.className='mycolor';
        </script>
    </body>
</html>

按CSS查詢一個 document.querySelector

按CSS查詢

示例1 查詢一個

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
<!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{
                text-decoration: initial;
            }
        </style>
    </head>
    <body>
        <ul id="ul">
            <li id="a">油條</li>
            <li id="b" class="steam">包子</li>
            <li id="c" class="steam">米餃</li>
            <li id="d"><a>魚粉</a></li>
        </ul>
        <script>
            // 返回一個元素節點
            var el = document.querySelector('li.steam');
            el.className='mycolor';
        </script>
    </body>
</html>

示例2 查詢多個

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
<!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{
                text-decoration: initial;
            }
        </style>
    </head>
    <body>
        <ul id="ul">
            <li id="a">油條</li>
            <li id="b" class="steam">包子</li>
            <li id="c" class="steam">米餃</li>
            <li id="d"><a>魚粉</a></li>
        </ul>
        <script>
            // 返回多個元素節點
            var els = document.querySelectorAll('li.steam');
            els[0].className='mycolor';
        </script>
    </body>
</html>

按元素標簽查詢多個 getElementsByTagName

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
<!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{
                text-decoration: initial;
            }
        </style>
    </head>
    <body>
        <ul id="ul">
            <li id="a">油條</li>
            <li id="b" class="steam">包子</li>
            <li id="c" class="steam">米餃</li>
            <li id="d"><a>魚粉</a></li>
        </ul>
        <script>
            // 按元素標簽查詢
            var els = document.getElementsByTagName('li');
            els[2].className='mycolor';
        </script>
    </body>
</html>

 

查詢父節點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
<!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{
                text-decoration: initial;
            }
        </style>
    </head>
    <body>
        <ul id="ul">
            <li id="a">油條</li>
            <li id="b" class="steam">包子</li>
            <li id="c" class="steam">米餃</li>
            <li id="d" ><a id="yufen">魚粉</a></li>
        </ul>
        <script>
            // 查找父節點
            var el = document.getElementById('yufen');
            el.parentNode.className="mycolor";
        </script>
    </body>
</html>

 

查找兄弟元素 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>這段代碼,要寫成這樣才正常查詢到。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM