HTML5向Web API新引入了 document.querySelector()和document.querySelectorAll()两个方法,都可以接收三种类型的参数:id(#),class(,),标签,就像jquery选择器,参数需要是合法的CSS选择语法。

用起来更方便的从DOM中选取元素,功能类似于jquery的选择器,这样在写原生js代码的时候就方便了许多。

document.querySelector()

返回文档中匹配指定的选择器组的第一个元素(使用深度优先先序遍历文档的节点 | 并且通过文档标记中的第一个元素,并按照子节点数量的顺序迭代顺序节点)。

语法:

element = document.querySelector(selectors);

其中:

1,element是一个element对象(DOM元素)。

2. selectors是一个字符串,包含一个或多个CSS选择器,多个则以逗号分割。

例子:这个例子中,会返回当前文档中第一个类名为‘myclass’的元素:

var el = document.querySelector(".myclass");
Document.querySelectorAll()

返回与指定的选择器组匹配的文档中的元素列表 (使用深度优先的先序遍历文档的节点)。返回的对象是 NodeList 。

语法:

elementList = document.querySelectorAll(selectors);

其中:

1, elementList是一个non-live的NodeList类型的对象;

2,selectors是由一个逗号连接的包含一个或多个CSS选择器的字符串。

如果 selectors参数中包含CSS伪元素,则返回一个空的elementList。

例子:

下面的例子返回一个文档中所有的class为"note"或者 "alert"的div元素.

var matches = document.querySelectorAll("div.note, div.alert");

document.querySelector()和document.querySelectorAll()的更多相关文章

  1. document.querySelector()与document.querySelectorAll()

      document.querySelector()与document.querySelectorAll() CreationTime--2018年7月1日10点49分 Author:Marydon ...

  2. 正确地缩写 document.querySelector

    北京的夕阳,伴随淡淡的霾殇.从写字楼望去,光线是那么昏黄.没有孤雁,也没有霞光,遥想当年,还是 jQuery 独霸一方.那时的我们,写程序都习惯了使用 $,至少在对美元符号的喜爱上,与 PHP 达成了 ...

  3. 实现兼容document.querySelector的方法

    var querySelector = function(selector) { //TODO 先简单兼容,后续继续扩展: var element = null; if(document.queryS ...

  4. document.querySelector和querySelectorAll方法

    querySelector和querySelectorAll是W3C提供的新的查询接口,其主要特点如下: 1.querySelector只返回匹配的第一个元素,如果没有匹配项,返回null.  2.q ...

  5. javascript之 原生document.querySelector和querySelectorAll方法

    querySelector和querySelectorAll是W3C提供的新的查询接口,其主要特点如下: 1.querySelector只返回匹配的第一个元素,如果没有匹配项,返回null.  2.q ...

  6. document.getElementById和document.querySelector的区别

    zepto中的$(".111")出错,jQuery中$(".111")不出错的原因: zepto用document.querySelector实现,jQuery ...

  7. document.getElementById & document.querySelector

    document.getElementById & document.querySelector https://developer.mozilla.org/en-US/docs/Web/AP ...

  8. 关于querySelector 和 document.getElementsByTagName 选中集合问题

    本文解决的问题是 :运用for..of..循环时,edge浏览器报Object doesn't support property or method 'symbol.iterator'问题 以及 符号 ...

  9. document.querySelector获取不到html标签对象实例的原因

    官方给出的HTML中的ID的命名规范: 1.必须以字母 A-Z 或 a-z 开头2.其后的字符:字母(A-Za-z).数字(0-9).连字符("-").下划线("_&qu ...

随机推荐

  1. Angularjs基础(十二)

    ng-model-options 描述:规定如何更新模型 实例: 在失去焦点时绑定输入框的值scope 变量中. <div ng-app="myApp" ng-control ...

  2. hdu_2588_GCD

    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...

  3. Eclipse关联tomcat

    一,添加Tomcat Windows-->Preferences-->Server-->Runtime Enviroment添加一个tomcat,这里选择tomcat8.0 Next ...

  4. SpringBoot配置redis和分布式session-redis

    springboot项目 和传统项目 配置redis的区别,更加简单方便,在分布式系统中,解决sesssion共享问题,可以用spring session redis. 1.pom.xml <d ...

  5. (第03节)三种ApplcationContext的实现

  6. jquery添加html代码的几种方法

    经常用jq来DOM添加html代码 就总结了jq里面最常用的动态添加html代码的方法 append在元素内部的尾部加上元素 prepend在元素内部的前部加上元素 after在元素外部的尾部加上元素 ...

  7. ELK 安装部署实战 (最新6.4.0版本)

    一.实战背景 根据公司平台的发展速度,对于ELK日志分析日益迫切.主要的需求有: 1.用户行为分析 2.运营活动点击率分析 作为上述2点需求,安装最新版本6.4.0是非常有必要的,大家可根据本人之前博 ...

  8. python3 练习题100例 (二十二)输入两个字符串,输出两个字符串集合的并集

    题目内容: 输入两个字符串,输出两个字符串集合的并集. 为保证输出结果一致,请将集合内元素排序之后再输出, 如对于集合aset,可输出sorted(aset). 输入格式: 共两行,每一行为一个字符串 ...

  9. Matplotlib 基本图表的绘制

    图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主 同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsize=None, use_index=T ...

  10. Python3爬虫(十) 数据存储之非关系型数据库MongoDB

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.非关系型数据库NoSQL全程是Not Only SQL,非关系型数据库.NoSQL是基于键值对的,不需要经过S ...