前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JavaScript选择器

JavaScript选择器

作者头像
WindrunnerMax
发布2020-08-27 10:21:51
7320
发布2020-08-27 10:21:51
举报
文章被收录于专栏:Czy‘s BlogCzy‘s Blog

Js选择器

JS选择器常用的有getElementById()getElementsByClassName()getElementsByName()getElementsByTagName()querySelector()querySelectorAll()

getElementById

通过id来定位,返回对指定id的第一个对象的引用,返回类型为HTMLDivElement

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>

知识相关

代码语言:javascript
复制
数组的遍历 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
复制
<!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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-04-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Js选择器
    • getElementById
      • getElementsByClassName
        • getElementsByName
          • getElementsByTagName
            • querySelector
              • querySelectorAll
                • 知识相关
                  • 代码示例
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档