xxxxxxxxxx
<!DOCTYPE html>
<html>
<title>JavaScript HTML DOM Selectors选择器示例 - 基础教程(div.cn)</title>
<head>
<style>
p {
border: 1px solid black;
padding: 14px 10px;
margin: 10px;
}
</style>
</head>
<body>
<p>这是一个P元素。 P的索引是0。</p>
<p>这是一个P元素。 P的索引是1。</p>
<p>这是一个P元素。 P的索引是2。</p>
<p>点击按钮更改所有P元素的背景颜色:</p>
<button onclick="myFunc()">点我试试</button>
<script>
function myFunc() {
var x = document.getElementsByTagName("p");
for(let i = 0; i < x.length; i++) {
x[i].style.backgroundColor = "coral";
}
}
</script>
</body>
</html>