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选择器_如何通过选择正确JavaScript选择器来避免沮丧

    javascript选择器 选择器如何影响代码的快速指南 (A quick guide on how selectors affect your code) While working on a pr ...

  2. js原生后代选择器_HTML5的JavaScript选择器介绍

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

  3. JavaScript选择器:getElementsByTagName

    The first of the "traditional" JavaScript selectors, getElementsByTagName does exactly wha ...

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

    前面的话 既然有getElementById()和getElementsByTagName()方法,为什么没有getElementsByClassName()呢?id属性.标签名.class属性并没有 ...

  5. HTML5新的javascript选择器

    新的选择器 document.querySelector("selector"); selector:根据CSS选择器返回第一个匹配到的元素,如果没有匹配到,则返回null; 支持 ...

  6. JavaScript选择器的使用说明

    (1)通配符选择器     *{ } (2)类型选择器     标签名{ } (3)id选择器     #id{ } (4)类选择器     .类名{ } (5)包含选择器     E1 E2{    ...

  7. javascript选择器、自定义属性

    一.选择器 document.getElementById('id名') document.getElementsByClassName('类名')[i]  一组  数组集合 document.get ...

  8. 解耦HTML、CSS和JavaScript

    当前在互联网上,任何一个稍微复杂的网站或者应用程序都会包含许多HTML.CSS和JavaScript.随着互联网运用的发展以及我们对它的依赖性日益增加,设定一个关于组织和维护你的前端代码的计划是绝对需 ...

  9. jQuery(JavaScript类库)

    文章目录 一.概述 1.1 介绍 1.2 网络资源 1.3 快速入门 二.选择器 2.1 介绍 2.2 JavaScript和jQuery选择器区别 三.事件 3.1 鼠标事件 四.操作Dom元素 学 ...

最新文章

  1. 入门单片机需要购买什么东西,学哪款单片机最好?
  2. js按钮触发网页提醒_jquery,js页面加载时自动点击触发jq按钮-Go语言中文社区
  3. end_form_tag 已经在rails2.x中去掉了
  4. 20个html标签及其作用,请写出至少20个html标签,并说说各个标签的功能或作用。...
  5. 使用Spring+Junit4.4进行测试
  6. Go的net/http
  7. 69. x 的平方根 golang
  8. Flask 第三方组件之 script
  9. 对于根目录磁盘满的了问题
  10. 这首歌,竟然是AI生成的,太狠了...
  11. winform 鼠标 静止时间_四款蓝牙鼠标的详细使用体验对比
  12. vue源码之数据侦测
  13. imagenet数据集类别标签和对应的英文中文对照表
  14. 认识计算机课件小游戏,2013年小学信息技术教案《认识计算机二》教案
  15. Chromedriver Mirror
  16. C语言第6次上课随堂讲义
  17. 2.11 求N分之一序列前N项和
  18. 欧拉公式-python程序-计算机仿真方法(入门级)
  19. python——答题系统
  20. php-cs-fixer实践

热门文章

  1. 详解 Qt 串口通信程序全程图文 (1)
  2. 好棒,测试妹子都能看懂的Jenkins Docker安装教程
  3. 其实Go 1.17 就支持泛型了,具体该怎么用呢?
  4. 使用jQuery来实现一个简单的ajax请求
  5. docker开启mysql的binlog日志
  6. 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  7. 人机交互,加速机器人拟人化
  8. 算法-两最长回文子串
  9. 关于多线程中锁的理解
  10. iOS 通用宏定义 高效全局宏汇总