JavaScript 查找元素

Posted max-hou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 查找元素相关的知识,希望对你有一定的参考价值。

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>

以上是关于JavaScript 查找元素的主要内容,如果未能解决你的问题,请参考以下文章

查找正在更改 DOM 元素的 javascript

JavaScript DOM基础:查找元素

JavaScript查找数组是否存在指定元素

JavaScript 学习-27.查找HTML DOM节点(元素)

Selenium c#如何通过JavaScript查找元素?

javascript 在数组中查找元素