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

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1 3.5 (1.9.1) 8 10 3.2 (525.3)
:scope pseudo-class (Yes) 32 No support 15[1] 7.0

[1] Supported in Opera 15+ by enabling the "Enable <style scoped>" or "Enable experimental Web Platform features" flag in chrome://flags.

See also

Document Tags and Contributors

 Last updated by: wbamberg, Dec 18, 2017, 8:46:15 PM

AngleSharp 的Dom 选择器的更多相关文章

  1. 都别说工资低了,我们来一起写简单的dom选择器吧!

    前言 我师父(http://www.cnblogs.com/aaronjs/)说应当阅读框架(jquery),所以老夫就准备开始看了 然后公司的师兄原来写了个dom选择器,感觉不错啊!!!原来自己从来 ...

  2. 关于一个新的DOM选择器querySelector

    在传统的javascript中,提到DOM选择器,大家比较熟悉的方式是通过tag,name,id来获取,其实大家都发现如果获取比较复杂的话,用这个方法会很繁琐,这时大家应该都会想到jquery里获取一 ...

  3. 一周学会Mootools 1.4中文教程:(1)Dom选择器

    利器: 君欲善其事须先利其器,好吧因为我们的时间比较紧迫,只有六天而已,那么六天的时间用死记硬背的方式学会Mt犹如天方夜谭,因此我们需要借鉴一下Editplus的素材栏帮我们记忆就好了,当我们需要用到 ...

  4. DOM选择器

    DOM选择器分为:id.class.name.tagname.高级.关系选择器;(返回的都是标签) 一:元素节点选择器: 1. id: 返回的是单个对象 <body> <div cl ...

  5. 订制DOM选择器

    本来是打算参考zepto.js,然后将里面想要的部分抽出来做函数,随调随用. 但后面发现这种写法重复代码太多,代码不整洁,于是就打算模仿下zepto的写法,挑出些比较实用的方法,造一下轮子. 起名叫“ ...

  6. 原生的强大DOM选择器querySelector

    在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...

  7. Dom选择器及操作文本内容

    文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把 ...

  8. 强大的原生DOM选择器querySelector和querySelectorAll

    在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...

  9. 原生JS强大DOM选择器querySelector与querySelectorAll

    在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id ...

随机推荐

  1. jquery each循环遍历完再执行的方法 因为each是异步的 所以要加计数器.

    query each循环遍历完再执行的方法因为each是异步的 所以要加计数器.var eachcount=0;$(“.emptytip”).each(function(){ eachcount++c ...

  2. google chrome 删除重复的书签 about sync

    之前 由于 谷歌 同步的不智能,且不询问用户同步方法和细节,导致我的书签包括了大量重复的书签,想去除重复的书签. 由于谷歌书签文件 存储在:C:\Documents and Settings\Admi ...

  3. C++ 反射机制的简单实现

    C++并不支持反射机制,只能自己实现. 如果需要实现字字符串到函数到映射,一定要使用到函数指针. 简单实现反射机制,根据字符串来构造相应到类.主要有以下几点: (1) 可以使用map保存字符从到函数指 ...

  4. Maximum Submatrix & Largest Rectangle

    相关题型 问题一(最大和子矩阵) : 有一个 m x n 的矩阵,矩阵的元素可正可负.请找出该矩阵的一个子矩阵(方块),使得其所有元素之和在所有子矩阵中最大.(问题来源:http://acm.pku. ...

  5. 10 个非常有用的 SVG 动画的 JavaScript 库

    SVG 通常可以用作跨分辨率视频.这意味着在一块高分屏幕上不会降低图片的锐度.此外,你甚至可以让SVG动起来,通过使用一些javascript类库.下面,我们分享一些javascript类库,这些类库 ...

  6. canvas移动端常用技巧图片loading

    核心知识点:drawImage 作用:将图片加载在canvas html: <canvas id="myCanvas" width="200" heigh ...

  7. 两个List合并去重

    今天遇到一个合并去重问题,从网上搜索一样总结出来两个比较简单的方法,这里去重是只能取出地址相同的数据,例如:如果两个字符串的值相同但都是单独new出来的这样去不了 @Test public void ...

  8. 转:CRF++词性标注

    CRF++词性标注 2016-02-28 分类:NLP 阅读(5558) 评论(19)  训练和测试的语料都是人民日报98年标注语料,训练和测试比例是10:1,直接通过CRF++标注词性的准确率:0. ...

  9. Windows 增强版任务管理器-Process Explorer

    百度百科PROCESS EXPLORER介绍 由Sysinternals开发的Windows系统和应用程序监视工具,目前已并入微软旗下.不仅结合了Filemon(文件监视器)和Regmon(注册表监视 ...

  10. SSAS知识回放之订单数据分析

    1:目标 基于已经做好的DW,利用SSAS实现一个多维数据模型的创建,通过浏览可以简单的实现订单数据的分析 2:步骤 2.1:添加数据源 如下图所示,创建一个数据仓库层的数据源连接 2.2:添加数据源 ...