登录  /  注册
博主信息
博文 39
粉丝 2
评论 2
访问量 49510
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
getElementById,getElementsByName,getElementsByTagName,getElementsByClassName四者之间的区别
fighting的博客
原创
977人浏览过

在《Javascript DOM 编程艺术》与 W3school 中是这样定义的:


getElementById():这个方法将返回一个与那个有着给定id属性值的元素节点对应的对象。


下面这个案例我是想通过触发test()函数后,给一个id为“div1”的div层添加背景色为黄颜色。


<!DOCTYPE html>

<html>

<head>

    <title>shopping list</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link href="css/css.css" rel="stylesheet" type="text/css">

</head>

    <h1>what to buy</h1>

    <P title="a gentle reminder">Don't forget to buy this stuff.</P>

    <ul id="purchases">

        <li>A tin of beans</li>

        <li class="sale">Cheese</li>

        <li class="sale important">milk</li>

    </ul>

    <div id="div1">helloworld</div>

    <input type="button" value="按钮" onclick="test()"/>

    <script>

        function test(){

            document.getElementById("div1").style.backgroundColor="yellow";

        }

    </script>

</body>

</html>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

输出结果:




getElementsByName():方法可返回带有指定名称的对象的集合。


这个案例是为获取无序列表中名字name叫“t”的li 的长度。


<!DOCTYPE html>

<html>

<head>

    <title>shopping list</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link href="css/css.css" rel="stylesheet" type="text/css">

</head>

    <h1>what to buy</h1>

    <P title="a gentle reminder">Don't forget to buy this stuff.</P>

    <ul id="purchases">

        <li name="t">A tin of beans</li>

        <li name="t" class="sale">Cheese</li>

        <li name="t" class="sale important">milk</li>

    </ul>

    <div id="div1">helloworld</div>

    <input type="button" value="按钮" onclick="test()"/>

    <script>

        function test(){

            alert(document.getElementsByName("t").length);

        }

    </script>

</body>

</html>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

输出结果:




getElementsByTagName():方法返回一个对象数组,每个对象分别对应着文档里有着给定标签的一个元素。这个理解起来有点拗口。 

W3school中是这样定义的:此方法可返回带有指定标签名的对象的集合。


下面这个我是要获得script标签的长度


<!DOCTYPE html>

<html>

<head>

    <title>shopping list</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link href="css/css.css" rel="stylesheet" type="text/css">

</head>

    <h1>what to buy</h1>

    <P title="a gentle reminder">Don't forget to buy this stuff.</P>

    <ul id="purchases">

        <li name="t" class="sale">A tin of beans</li>

        <li name="t" class="sale">Cheese</li>

        <li name="t" class="sale important">milk</li>

    </ul>

    <div id="div1">helloworld</div>

    <input type="button" value="按钮" onclick="test()"/>

    <script>

        function test(){

            alert(document.getElementsByTagName("script").length);

        }

    </script>

</body>

</html>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

输出结果:




getElementsByClassName():方法返回文档中所有指定类名的元素集合,作为 NodeList 对象。


下面这个是要设置class=”sale”且数组下标为:1的li的背景色为:red


<!DOCTYPE html>

<html>

<head>

    <title>shopping list</title>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <link href="css/css.css" rel="stylesheet" type="text/css">

</head>

    <h1>what to buy</h1>

    <P title="a gentle reminder">Don't forget to buy this stuff.</P>

    <ul id="purchases">

        <li name="t" class="sale">A tin of beans</li>

        <li name="t" class="sale">Cheese</li>

        <li name="t" class="sale important">milk</li>

    </ul>

    <div id="div1">helloworld</div>

    <input type="button" value="按钮" onclick="test()"/>

    <script>

        function test(){

            var x=document.getElementsByClassName("sale");

            x[1].style.backgroundColor="red";

        }

    </script>

</body>

</html>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

输出结果:




批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学