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

JavaScript选择器的更多相关文章

  1. 深入理解javascript选择器API系列第三篇——h5新增的3种selector方法

    × 目录 [1]方法 [2]非实时 [3]缺陷 前面的话 尽管DOM作为API已经非常完善了,但是为了实现更多的功能,DOM仍然进行了扩展,其中一个重要的扩展就是对选择器API的扩展.人们对jQuer ...

  2. jQuery 选择器和JavaScript 选择器的技巧与异常原因

    jquery的选择器借鉴了css选择器,核心依然依靠JavaScript的getElementById()和getElementsByTagName()方法,但是他封装了2个方法,让jquery选择器 ...

  3. 深入理解javascript选择器API系列第三篇——HTML5新增的3种selector方法

    前面的话 尽管DOM作为API已经非常完善了,但是为了实现更多的功能,DOM仍然进行了扩展,其中一个重要的扩展就是对选择器API的扩展.人们对jQuery的称赞,很多是由于jQuery方便的元素选择器 ...

  4. 使用HTML5的JavaScript选择器操作页面中的元素

    <!doctype html><html lang="en"> <head>     <meta charset="UTF-8& ...

  5. HTML5的JavaScript选择器介绍

    在HTML5出现之前使用JavaScript查找DOM元素,有以下三种原生的方法: getElementById:根据指定元素的id属性返回元素 getElementsByName:返回所有指定nam ...

  6. JavaScript选择器和节点操作

    感谢:链接(视频讲解很清晰) 下文中讲解用到Chrome中的console调试台,如果不懂最好先看一下:链接 JavaScript选择器 作用:选取html中的标签等内容,最重要的还是为节点的操作(增 ...

  7. 深入理解javascript选择器API系列第二篇——getElementsByClassName

    × 目录 [1]使用 [2]classList [3]扩展 前面的话 既然有getElementById()和getElementsByTagName()方法,为什么没有getElementsByCl ...

  8. 深入理解javascript选择器API系列第一篇——4种元素选择器

    × 目录 [1]id属性 [2]标签名 [3]name属性[4]all 前面的话 说到最常见的DOM应用,恐怕就要数取得特定的某个或某组元素的引用了.DOM定义了许多方式来选取元素,包括getElem ...

  9. javascript选择器querySelector和querySelectorAll的使用和区别

    querySelector 和 querySelectorAll 方法是 W3C Selectors API规范中定义的.他们的作用是根据 CSS 选择器规范,便捷定位文档中指定元素. 目前几乎主流浏 ...

  10. 深入学习jQuery选择器系列第八篇——过滤选择器之伪子元素选择器

    × 目录 [1]通用形式 [2]反向形式 [3]首尾元素 [4]唯一元素 前面的话 本文是子元素选择器的续篇,主要介绍关于nth-of-type()选择器的内容.该部分内容并非没有出现在<锋利的 ...

随机推荐

  1. CSS - 正确解决 float 高度坍塌的问题

    <!DOCTYPE html> <html lang="en" dir="ltr"> <head> <meta cha ...

  2. 一次异常OOM问题学习跟踪的过程

    摘要 春节后第一周一个项目出现了OOM的问题. 平台研发和产品研发跟踪了接近一周的时间也没有最终确认问题根因. 这里总结一下整个过程, 希望以后在遇到相同问题时会有进一步的结论. 产品的稳定运行离不开 ...

  3. [转帖]crash工具分析Kdump下vmcore文件常用命令总结(三)(实例易懂)

    一.简介 本文主要介绍使用crash工具对kdump生成的vmcore文件进行分析,解析常见的crash命令,前面已讲述两章关于Kdump的内容,读者感兴趣可以点击下面的链接: 1.Kdump调试机理 ...

  4. [转帖]shell命令替换~date用法~如果被替换命令的输出内容包括多行或有多个连续的空白符,输出变量时应该将变量用双引号包围

    https://www.cnblogs.com/mianbaoshu/p/12069458.html Shell 命令替换是指将命令的输出结果赋值给某个变量.比如,将使用ls命令查看到的某个目录中的内 ...

  5. [转帖]⭐万字长篇超详细的图解Tomcat中间件方方面面储备知识⭐

    https://developer.aliyun.com/article/885079?spm=a2c6h.24874632.expert-profile.321.7c46cfe9h5DxWK 202 ...

  6. ELK搭建简单说明

    ELK搭建简单说明 简单说明 周天陪孩子学习,自己简单研究了下如何处理 不是很系统,仅是能够展示部分文件的日志. 需要有很多坑需要进行填充. 搭建模式 自己采取rpm包方式进行搭建 183 机器上面安 ...

  7. Springboot数据库连接池的学习与了解

    背景 昨天学习总结了tomcat的http连接池和线程池相关的知识,总结的不是很完整, 自己知道的也比较少,总结的时候就在想tomcat针对client 端有连接池,并且通过NIO的机制, 以较少的t ...

  8. 2022 倒带 - NutUI

    作者:京东零售 于明明 前言 时光飞逝,流年似水,让我们倒带 2022,回首这跌宕起伏一年走过的 "升级之路". NutUI 表现如何? 成绩单等着您打分! 2022 是 NutU ...

  9. 你不知道的Linux shell操作

    Linux Shell 脚本入门教程 Linux Shell 脚本是一种强大的工具,它允许您自动化日常任务和复杂操作.在本教程中,我们将逐步介绍几个实用的 Shell 脚本示例.每个示例都将详细说明, ...

  10. 【一个小发现】VictoriaMetrics 中 vmselect 的 `-search.denyPartialResponse` 选项不应该开启

    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 一直以为vmselect 的 -search.denyPa ...