HTML DOM getElementsByClassName() 方法 | DOM 对象:HTML DOM 元素对象

返回到:DOM 对象:HTML DOM 元素对象

定义和使用

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

NodeList 对象代表一个有顺序的节点列表。NodeList 对象 我们可通过节点列表中的节点索引号来访问列表中的节点(索引号由0开始)。

提示: 你可以使用 NodeList 对象的 length 属性来确定指定类名的元素个数,并循环各个元素来获取你需要的那个元素。

浏览器支持

表格中的数字表示支持该方法的第一个浏览器的版本号。

方法谷歌IE火狐苹果opera
getElementsByClassName()4.09.03.03.19.5

语法

element.getElementsByClassName(classname)

参数值

参数类型描述
classnameString必须。你需要获取的元素类名。

多个类名使用空格分隔,如 “test demo”。

技术描述

DOM Version:Core Level 1 Element Object
Return Value:NodeList 对象,表示指定类名的元素集合。元素在集合中的顺序以其在代码中的出现次序排序。

实例

在 class=”example” 的列表中修改 class=”child” 的第一项(索引值为 0) 的文:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>

<ul class="example">
  <li class="child">Coffee</li>
  <li class="child">Tea</li>
</ul>
<p>点击按钮修改第一个列表项的文本信息 (索引值为 0)。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 8 及更早 IE 版本不支持 getElementsByClassName() 方法。</p>
<script>
function myFunction() {
    var list = document.getElementsByClassName("example")[0];
    list.getElementsByClassName("child")[0].innerHTML = "Milk";
}
</script>

</body>
</html>

修改 <div> 元素中第二个 class=”child” 元素的背景颜色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>

<ul class="example">
  <li class="child">Coffee</li>
  <li class="child">Tea</li>
</ul>
<p>点击按钮修改第一个列表项的文本信息 (索引值为 0)。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 8 及更早 IE 版本不支持 getElementsByClassName() 方法。</p>
<script>
function myFunction() {
    var list = document.getElementsByClassName("example")[0];
    list.getElementsByClassName("child")[0].innerHTML = "Milk";
}
</script>

</body>
</html>

查看 <div> 元素中有多少个 class=”child” 的元素 (使用 NodeList 的 length 属性):

var x = document.getElementById("myDIV").getElementsByClassName("child").length;


//
3

修改class=”example”元素中第一个类名为 “child” 和 “color” 元素的背景颜色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
<style>
div {
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>

<div class="example">
  <p class="child">在 class="example" 的第一个 div 元素中 class="child" 的 P 元素。  P 的索引值为 0,Div 的索引值为 0。</p>
</div>
<div class="example">
  <p class="child color">
	在 class="example" 的第二个 div 元素中 class="child color" 的第一个 P 元素。  P 的索引值为 0,Div 的索引值为 1。
  </p>
  <p class="child color">
  在 class="example" 的第二个 div 元素中 class="child color" 的第二个 P 元素。  P 的索引值为 1,Div 的索引值为 1。	
  </p>
</div>
<div class="example">
  <p class="child color">在 class="example" 的第三个 div 元素中 class="child color" 的 第一个P 元素。  P 的索引值为 0,Div 的索引值为 2。</p>
  <p class="child color">在 class="example" 的第三个 div 元素中 class="child color" 的 第二个P 元素。  P 的索引值为 1,Div 的索引值为 2。</p>
</div>
<p>点击按钮修改 class="example" 的 div 元素中类名为 "child" 和 "color"的第一个 p 元素。</p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 8 及更早 IE 版本不支持 getElementsByClassName() 方法。</p>
<script>
function myFunction() {
    var x = document.getElementsByClassName("example")[1];
    x.getElementsByClassName("child color")[0].style.backgroundColor = "red";
}
</script>

</body>
</html>

修改 <div> 元素中 class=”child” 的所有元素背景颜色:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
<style>
div {
    border: 1px solid black;
    margin: 5px;
}
</style>
</head>
<body>

<div id="myDIV"> 
  <p class="child"> div 元素中 class="child" 的 p </p>
  <p class="child"> div 元素中 class="child" 的另外一个 p </p>
  <p>在 div 中的 p 元素插入 <span class="child">class="child" 的 span 元素</span> 。</p>
</div>
<p>点击按钮为 id="myDIV" 的 div 元素中所有 class="child" 元素添加背景颜色。 </p>
<button onclick="myFunction()">点我</button>
<p><strong>注意:</strong> Internet Explorer 8 及更早 IE 版本不支持 getElementsByClassName() 方法。</p>
<script>
function myFunction() {
    var x = document.getElementById("myDIV");
    var y = x.getElementsByClassName("child");
    var i;
    for (i = 0; i < y.length; i++) {
        y[i].style.backgroundColor = "red";
    }
}
</script>

</body>
</html>

作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4395.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2021年12月7日 下午4:20
下一篇 2021年12月10日 上午11:28

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注