JavaScript选择器
Js选择器
JS选择器常用的有getElementById()
、getElementsByClassName()
、getElementsByName()
、getElementsByTagName()
、querySelector()
、querySelectorAll()
。
getElementById#
通过id
来定位,返回对指定id
的第一个对象的引用,返回类型为HTMLDivElement
。
<div id="t1">T1</div>
<script type="text/javascript">
var t1 = document.getElementById("t1");
console.log(t1); // <div id="t1">D1</div>
console.log(Object.prototype.toString.call(t1)); // [object HTMLDivElement]
</script>
getElementsByClassName#
通过class
属性来定位,返回文档中指定class
属性值的元素的引用,返回类型为HTMLCollection
。
<div class="t2">D2</div>
<div class="t2">D3</div>
<script type="text/javascript">
var t2List = document.getElementsByClassName("t2");
console.log(t2List); // HTMLCollection(2) [div.t2, div.t2]
// 使用for循环遍历
for(let i=0,n=t2List.length;i<n;++i) console.log(t2List[i]);
// HTMLCollection的prototype中没有forEach方法,遍历需要使用Array的prototype中forEach通过call绑定对象实例并传参
Array.prototype.forEach.call(t2List,v => console.log(v) );
// HTMLCollection的prototype中没有map方法,也需要使用Array的prototype中forEach通过call绑定对象实例并传参
Array.prototype.map.call(t2List,v => console.log(v) );
</script>
getElementsByName#
通过name
属性来定位,返回文档中指定name
属性值的元素的引用,返回类型为NodeList
。
<div name="t3">D4</div>
<div name="t3">D5</div>
<script type="text/javascript">
var t3List = document.getElementsByName("t3");
console.log(t3List); // NodeList(2) [div, div]
// 可直接使用forEach进行遍历
t3List.forEach( v => console.log(v) );
// NodeList的prototype中没有map方法,使用map的场景也需要借助Array的prototype中map通过call绑定对象实例并传参
Array.prototype.map.call(t3List,v => console.log(v) );
</script>
getElementsByTagName#
通过标签的名字来定位,返回文档中指定标签的元素的引用,返回类型为HTMLCollection
。
<p class="t4">P6</p>
<p class="t4">P7</p>
<script type="text/javascript">
var t4List = document.getElementsByTagName("p");
console.log(t4List); // HTMLCollection(2) [p, p]
Array.prototype.forEach.call(t4List, function(v){console.log(v);});
Array.prototype.map.call(t4List,function(v){console.log(v);} );
</script>
querySelector#
通过CSS
选择器来定位,返回文档中匹配指定CSS
选择器的第一个元素的引用,返回类型为HTMLDivElement
。
<div>
<div class="t5">D8</div>
</div>
<script type="text/javascript">
var t5 = document.querySelector("div .t5");
console.log(t5); // <div class="t5">D8</div>
console.log(Object.prototype.toString.call(t5)); // [object HTMLDivElement]
</script>
querySelectorAll#
通过CSS
选择器来定位,返回文档中匹配指定CSS
选择器的所有元素的引用,返回类型为NodeList
。
<div>
<div id="t6">D9</div>
<div>D10</div>
<div>D11</div>
</div>
<script type="text/javascript">
var t6List = document.querySelectorAll("#t6 ~ div");
console.log(t6List); // NodeList(2)[div, div]
t6List.forEach(function(v){console.log(v);});
Array.prototype.map.call(t6List,function(v){console.log(v);} );
</script>
代码示例#
<!DOCTYPE html>
<html>
<head>
<title>Js选择器</title>
<meta charset="utf-8">
</head>
<body>
<div id="t1">D1</div>
<div class="t2">D2</div>
<div class="t2">D3</div>
<div name="t3">D4</div>
<div name="t3">D5</div>
<p class="t4">P6</p>
<p class="t4">P7</p>
<div>
<div class="t5">D8</div>
</div>
<div>
<div id="t6">D9</div>
<div>D10</div>
<div>D11</div>
</div>
</body>
<script type="text/javascript">
var t1 = document.getElementById("t1");
console.log(t1); // <div id="t1">D1</div>
console.log(Object.prototype.toString.call(t1)); // [object HTMLDivElement]
console.log("");
var t2List = document.getElementsByClassName("t2");
console.log(t2List); // HTMLCollection(2) [div.t2, div.t2]
// 使用for循环遍历
for(let i=0,n=t2List.length;i<n;++i) console.log(t2List[i]);
// HTMLCollection的prototype中没有forEach方法,遍历需要使用Array的prototype中forEach通过call绑定对象实例并传参
Array.prototype.forEach.call(t2List,v => console.log(v) );
// HTMLCollection的prototype中没有map方法,也需要使用Array的prototype中forEach通过call绑定对象实例并传参
Array.prototype.map.call(t2List,v => console.log(v) );
console.log("");
var t3List = document.getElementsByName("t3");
console.log(t3List); // NodeList(2) [div, div]
// 可直接使用forEach进行遍历
t3List.forEach( v => console.log(v) );
// NodeList的prototype中没有map方法,使用map的场景也需要借助Array的prototype中map通过call绑定对象实例并传参
Array.prototype.map.call(t3List,v => console.log(v) );
console.log("");
var t4List = document.getElementsByTagName("p");
console.log(t4List); // HTMLCollection(2) [p, p]
Array.prototype.forEach.call(t4List, function(v){console.log(v);});
Array.prototype.map.call(t4List,function(v){console.log(v);} );
console.log("");
var t5 = document.querySelector("div > .t5");
console.log(t5); // <div class="t5">D8</div>
console.log(Object.prototype.toString.call(t5)); // [object HTMLDivElement]
console.log("");
var t6List = document.querySelectorAll("#t6 ~ div");
console.log(t6List); // NodeList(2) [div, div]
t6List.forEach(function(v){console.log(v);});
Array.prototype.map.call(t6List,function(v){console.log(v);} );
console.log("");
</script>
</html>
每日一题#
https://github.com/WindrunnerMax/EveryDay
参考#
数组的遍历 https://github.com/WindrunnerMax/EveryDay/blob/master/JavaScript/Js%E9%81%8D%E5%8E%86%E6%95%B0%E7%BB%84%E6%80%BB%E7%BB%93.md
ES6箭头函数 https://github.com/WindrunnerMax/EveryDay/blob/master/JavaScript/ES6%E6%96%B0%E7%89%B9%E6%80%A7.md
原型与原型链 https://github.com/WindrunnerMax/EveryDay/blob/master/JavaScript/%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE.md
CSS选择器 https://github.com/WindrunnerMax/EveryDay/blob/master/CSS/CSS%E9%80%89%E6%8B%A9%E5%99%A8.md
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理