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

SpecificationStatusComment
Selectors API Level 1
The definition of 'querySelectorAll' in that specification.
ObsoleteInitial definition

Browser compatibility

  • Desktop  
  • Mobile
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support13.5 (1.9.1)8103.2 (525.3)
:scope pseudo-class(Yes)32No support15[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:  
 Last updated by: wbamberg, Dec 18, 2017, 8:46:15 PM
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值