AngleSharp 的Dom 选择器
AngleSharp
https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll
Element.querySelectorAll()
Returns a static (not live) NodeList
of all elements descended from the element on which it is invoked that matches the specified group of CSS selectors. (The base element itself is not included, even if it matches.)
Note: The definition of this API was moved to the ParentNode
interface.
Syntax
elementList = baseElement.querySelectorAll(selectors);
where:
elementList
- is a non-live node list [
NodeList[elements]
]
of element objects. baseElement
- is an element object.
selectors
- is a group of selectors to match on or elements of the DOM.
Examples
This example returns a list of all the p
elements in the HTML document body:
let matches = document.body.querySelectorAll('p');
This example returns a list of p
children elements under a container, whose parent is a div
that has the class 'highlighted':
let el = document.querySelector('#test'); //return an element with id='test'
let matches = el.querySelectorAll('div.highlighted > p'); // return a NodeList of p wrapped in a div with attribute class "highlighted"
This example returns a list of iframe
elements that contain a attribute 'data-src':
let matches = el.querySelectorAll('iframe[data-src]');
Notes
If the specified "selectors" are not found inside the DOM of the page, the method queryselectorAll
returns an empty NodeList as specified below:
> let x = document.body.querySelectorAll('.highlighted'); //case: if the class highlighted doesn't exist in any attribute "class" of the DOM the result is
> [] //empty NodeList
querySelectorAll()
was introduced in the WebApps API.
The string argument pass to querySelectorAll
must follow the CSS syntax. See document.querySelector
for a concrete example.
We could access a single item inside the NodeList in the following way:
let x = document.body.querySelectorAll('.highlighted');
x.length; //return the size of x
x[i_item]; //where i_item has a value between 0 and x.length-1. The operator "[]" return as in an array the element at index "i_item"
We could iterate inside a NodeList with the construct for(....) {...}
as in the following code:
let x = document.body.querySelectorAll('.highlighted');
let index = 0;
for( index=0; index < x.length; index++ ) {
console.log(x[index]);
}
So in the above way, it is possible to manage and modify the behaviour of the page.
Quirks
querySelectorAll()
behaves differently than most common JavaScript DOM libraries, which might lead to unexpected results:
<div class="outer">
<div class="select">
<div class="inner">
</div>
</div>
</div>
let select = document.querySelector('.select');
let inner = select.querySelectorAll('.outer .inner');
inner.length; // 1, not 0!
In this example, when selecting .outer .inner
in the context of .select
, .inner
is still found, even though .outer
is not a descendant of the baseElement (.select
).querySelectorAll()
only verifies that the last element in the selector is within the baseElement.
The :scope
pseudo-class restores the expected behavior, only matching selectors on descendants of the baseElement:
let select = document.querySelector('.select');
let inner = select.querySelectorAll(':scope .outer .inner');
inner.length; // 0
Specifications
Specification | Status | Comment |
---|---|---|
Selectors API Level 1 The definition of 'querySelectorAll' in that specification. |
Obsolete | Initial definition |
Browser compatibility
[1] Supported in Opera 15+ by enabling the "Enable <style scoped>" or "Enable experimental Web Platform features" flag in chrome://flags
.
See also
element.querySelector()
document.querySelectorAll()
document.querySelector()
- Code snippets for
querySelector
Document Tags and Contributors
AngleSharp 的Dom 选择器的更多相关文章
- 都别说工资低了,我们来一起写简单的dom选择器吧!
前言 我师父(http://www.cnblogs.com/aaronjs/)说应当阅读框架(jquery),所以老夫就准备开始看了 然后公司的师兄原来写了个dom选择器,感觉不错啊!!!原来自己从来 ...
- 关于一个新的DOM选择器querySelector
在传统的javascript中,提到DOM选择器,大家比较熟悉的方式是通过tag,name,id来获取,其实大家都发现如果获取比较复杂的话,用这个方法会很繁琐,这时大家应该都会想到jquery里获取一 ...
- 一周学会Mootools 1.4中文教程:(1)Dom选择器
利器: 君欲善其事须先利其器,好吧因为我们的时间比较紧迫,只有六天而已,那么六天的时间用死记硬背的方式学会Mt犹如天方夜谭,因此我们需要借鉴一下Editplus的素材栏帮我们记忆就好了,当我们需要用到 ...
- DOM选择器
DOM选择器分为:id.class.name.tagname.高级.关系选择器;(返回的都是标签) 一:元素节点选择器: 1. id: 返回的是单个对象 <body> <div cl ...
- 订制DOM选择器
本来是打算参考zepto.js,然后将里面想要的部分抽出来做函数,随调随用. 但后面发现这种写法重复代码太多,代码不整洁,于是就打算模仿下zepto的写法,挑出些比较实用的方法,造一下轮子. 起名叫“ ...
- 原生的强大DOM选择器querySelector
在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...
- Dom选择器及操作文本内容
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...
- 强大的原生DOM选择器querySelector和querySelectorAll
在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...
- 原生JS强大DOM选择器querySelector与querySelectorAll
在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...
随机推荐
- 8 个基于 Lucene 的开源搜索引擎推荐
Lucene是一种功能强大且被广泛使用的搜索引擎,以下列出了8种基于Lucene的搜索引擎,你可以想象它们有多么强大. 1. Apache Solr Solr 是一个高性能,采用Java5开发,基于L ...
- AVL树原理及实现 +B树
1. AVL定义 AVL树是一种改进版的搜索二叉树.对于一般的搜索二叉树而言,如果数据恰好是按照从小到大的顺序或者从大到小的顺序插入的,那么搜索二叉树就对退化成链表,这个时候查找,插入和删除的时间都会 ...
- 深入浅出Nodejs读书笔记
深入浅出Nodejs读书笔记 转:http://tw93.github.io/2015-03-01/shen-ru-qian-chu-nodejs-reading-mind-map.html cate ...
- C#基础知识整理:C#类和结构(1)
1.结构功能特性? 实现代码?结构用struct关键字定义的,与类类似,但有本质区别.结构实质是一个值类型,它不需要对分配的.结构的特性:(1).结构作为参数传递时,是值传递.(2).结构的构造函数必 ...
- Annotation Type EnableTransactionManagement
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/transaction/annotation/Ena ...
- struts2 18拦截器详解(九)
ScopedModelDrivenInterceptor 该拦截器处于defaultStack第八的位置,其主要功能是从指定的作用域内检索相应的model设置到Action中,该类中有三个相关的属性: ...
- MySql查询时间段的方法(转)
http://www.jb51.net/article/58668.htm 本文实例讲述了MySql查询时间段的方法.分享给大家供大家参考.具体方法如下: MySql查询时间段的方法未必人人都会,下面 ...
- Android Studio 上传aar(Library)到JCenter
目的 这篇文章介绍通过Gradle把开源项目发布到公共仓库JCenter中,发布自己的android library(也就是aar)到公共的jcenter仓库. 为什么选择JCenter,因为JCen ...
- Code optimization and organization in Javascript / jQuery
This article is a combined effort of Innofied Javascript developers Puja Deora and Subhajit Ghosh) W ...
- Ajax:HyperText/URI, HTML, Javascript, frame, frameset, DHTML/DOM, iframe, XMLHttp, XMLHttpRequest
本文内容 Ajax 诞生 促使 Ajax 产生的 Web 技术演化 真正 Ajax Ajax 与 Web 2.0 Ajax 背后的技术 2008 年毕业,2011 年看了<Ajax 高级程序设计 ...