定义与用法
querySelector() 方法返回匹配指定 CSS 选择器元素的第一个子元素 。
注意: querySelector() 方法只返回匹配指定选择器的第一个元素。如果你要返回所有匹配元素,请使用 querySelectorAll() 方法替代。
浏览器支持
表格中的数字表示支持该方法的第一个浏览器的版本号。
| 方法 | 谷歌 | IE | 火狐 | 苹果 | opera |
|---|---|---|---|---|---|
| querySelector() | 4.0 | 8.0 | 3.5 | 3.1 | 10.0 |
语法
element.querySelector(CSS 选择器)参数值
| 参数 | 类型 | 描述 |
|---|---|---|
| CSS 选择器 | String | 必须。指定一个或多个匹配元素的 CSS 选择器。 可以使用它们的 id, 类, 类型, 属性, 属性值等来选取元素。 对于多个选择器,使用逗号隔开,返回一个匹配的元素。 |
技术细节
| DOM 版本: | Selectors Level 1 Element Object |
|---|---|
| 返回值: | 匹配指定 CSS 选择器的第一个元素。 如果没有找到,返回 null。如果指定了非法选择器则 抛出 SYNTAX_ERR 异常。 |
实例
修改 class=”demo” 的 <div> 元素的第一个子元素文本内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>
<div id="myDIV">
<p class="demo">这是一个 div 中 class="demo" 的 p 元素。</p>
<p class="demo">这也是一个 div 中 class="demo" 的 p 元素。</p>
</div>
<p>点击按钮修改 div 元素中第一个 p 元素的文本内容。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
x.querySelector(".demo").innerHTML = "Hello World!";
}
</script>
</body>
</html>修改 <div> 元素中第一个 class=”example” 的子元素内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>
<div id="myDIV">
<h2 class="example">div 中 class="example" 的标题</h2>
<p class="example">div 中 class="example" 的段落。</p>
</div>
<p>点击按钮修改过 div 中的第一个 class="example" 的元素。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
x.querySelector(".example").innerHTML = "Hello World!";
}
</script>
</body>
</html>修改 <div> 元素中第一个 class=”example” 的 <p> 元素:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>
<div id="myDIV">
<h2 class="example">div 中 class="example" 的标题</h2>
<p class="example">div 中 class="example" 的段落。</p>
</div>
<p>点击按钮修改过 div 中的第一个 class="example" 的 p 元素。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
x.querySelector("p.example").innerHTML = "Hello World!";
}
</script>
</body>
</html>为 <div> 元素中的第一个有 target 属性的 <a> 元素添加红色边框:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
<style>
a[target] {
background-color: yellow;
}
div {
border: 1px solid black;
}
</style>
</head>
<body>
<p> CSS 选择器 a[target] 确保所有带有 target 属性的背景色设置为黄色:</p>
<div id="myDIV">
div块中有3个链接:
<a href="//www.w3cschool.cc">w3cschool.cc</a>
<a href="//www.disney.com" target="_blank">disney.com</a>
<a href="//www.wikipedia.org" target="_top">wikipedia.org</a>
</div>
<p>点击按钮为 div 元素中带有 target 属性的第一个属性设置红色边框。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction() {
var x = document.getElementById("myDIV");
x.querySelector("a[target]").style.border = "10px solid red";
}
</script>
</body>
</html>以下实例演示了多个选择器的使用方法。
假定你选择了两个选择器: <h2> 和 <h3> 元素。
以下代码将为 <div> 元素的第一个 <h2> 元素添加背景颜色:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>
<div id="myDIV">
<h2> h2 元素</h2>
<h3> h3 元素</h3>
</div>
<script>
var x = document.getElementById("myDIV");
x.querySelector("h2, h3").style.backgroundColor = "red";
</script>
</body>
</html>但是,如果 <div> 元素中 <h3> 元素位于 <h2> 元素之前,<h3> 元素将会被设置指定的背景颜色。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Web176教程网(web176.com)</title>
</head>
<body>
<div id="myDIV">
<h3> h3 元素</h3>
<h2> h2 元素</h2>
</div>
<script>
var x = document.getElementById("myDIV");
x.querySelector("h2, h3").style.backgroundColor = "red";
</script>
</body>
</html>作者:terry,如若转载,请注明出处:https://www.web176.com/javascriptbook/domtips/4508.html
支付宝
微信