AngleSharp 的Dom 选择器

AngleSharp

https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll

Element.querySelectorAll()

Jump to:

  1. Syntax
  2. Examples
  3. Notes
  4. Quirks
  5. Specifications
  6. Browser compatibility
  7. See also

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

Tags:

Contributors to this page: wbambergRobg1faxchanSebastianzyurikhanlornlyturtlenilssolankirobtaussigKonrudMrDanielLewisKevan,SpaceMudgeJeremiefscholzteoliHavvykscarfoneiamanupmenonSheppykimblimmattbastaethertankziyunfeiMarcoosebidelernestd,paul.irishMattBrubeckmyakuraJürgen JekaBrettz9Bzbarsky

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

时间: 2024-08-03 14:13:52

AngleSharp 的Dom 选择器的相关文章

DOM选择器

dom选择器 直接查找 document.getElementById('i1') 间接查找: 文本内容操作: innerText 仅文本 innerHTML 全内容 value <input type="text" value="python"/> 获取单钱标签中的值 select 获取选中的value <select id="xx"></select> <options></opacity

JavaScript DOM 选择器 querySelector

W3C的规范与库中的实现 querySelector:return the first matching Element node within the node’s subtrees. If there is no such node, the method must return null .(返回指定元素节点的子树中匹配selector的集合中的第一个,如果没有匹配,返回null) querySelectorAll:return a NodeList containing all of t

介绍一下Mojolicious的DOM选择器Mojo::DOM和它的Mojo::UserAgent(比较Web::Scraper)

最近正好又需要做页面分析,以前全是用AnyEvent::HTTP和Web::Scraper.这次试了试Mojo::DOM和Mojo::UserAgent. 先说结论,我的试用结论是:如果程序不和web沾边,只是个页面分析或文件处理程序,那还是前者好.否则的话可以考虑Mojo. 先说Mojo::DOM和Mojo::UserAgent的优点: Mojo::DOM做的这个dom选择器在一些时候是非常方便的 读入HTML以后可以精确定位需要的元素或是用回调的方式遍历. my $dom = Mojo::D

Dom选择器的使用

文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是,DOM把网页和脚本以及其他的编程语言联系了起来.DOM属于浏览器,而不是JavaScript语言规范里的规定的核心内容. 注:一般说的JS让页面动起来泛指JavaScript和Dom 1.选择器--id属性选择器 <!DOCTYPE html><html lang="en">

原生的强大DOM选择器querySelector

在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id 等方式来查找,这显然是远远不够的,如果想要进行更为精确的选择不得不使用看起来非常繁琐的正则表达式,或者使用某个库.事实上,现在所有的浏览器厂商都提供了 querySelector 和 querySelectorAll 这两个方法的支持,甚至就连微软也派出了 IE 8 作为支持这一特性的代表,quer

原生DOM选择器querySelector和querySelectorAll

在传统的 JavaScript 开发中,查找 DOM 往往是开发人员遇到的第一个头疼的问题,原生的 JavaScript 所提供的 DOM 选择方法并不多,仅仅局限于通过 tag, name, id 等方式来查找,这显然是远远不够的,如果想要进行更为精确的选择不得不使用看起来非常繁琐的正则表达式,或者使用某个库.事实上,现在所有的浏览器厂商都提供了 querySelector 和 querySelectorAll 这两个方法的支持,甚至就连微软也派出了 IE 8 作为支持这一特性的代表,quer

第15天 html css JavaScript dom选择器 示例左侧菜单

CSS补充: - position - background - hover - overflow - z-index - opacity 示例:输入框右边放置图标 JavaScript: 局部变量 var 基本数据类型: 数字 字符串 数组 字典 布尔值 For循环 条件语句 == != === !== || && 函数的定义: function func(){ ... }Dom 找标签 - 直接找 $('#id') $('.c1').siblings() - 简介找 操作: inner

Dom选择器使用与调试记录

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="#12">我喜欢学习python</div> <a>123</a> <a>45

Dom选择器--内容文本操作

一.文本内容操作 内容: <body> <div id="i1"> 学习是我快乐? <a>晚饭吃什么</a> </div> </body> 1.1 innerText 仅仅获取文本内容 var obj = document.getElementById("i1") obj <div id=?"i1">?…?</div>? obj.innerText &