Finding Things

The primary entry points into DOM are document.query() and document.queryAll(). Both take a CSS selector as the argument. The former returns a single matching element; the latter returns a list of all matching elements. Here are some simple examples:

dom/finding.dart
 
document.query(​'h1'​); ​// => First <h1> in the document
 
document.query(​'#people-list'​); ​// => Element with id of 'people-list'
 
document.query(​'.active'​); ​// => First element with 'active' class
 
document.queryAll(​'h2'​); ​// => All <h2> elements

The query and queryAll methods are actually methods of the Element class. Document, like every other class representing a bit of the DOM, subclasses Element. In practice, this means we can limit queries to a specific element by first finding the element and then querying from it.

 
var​ list = document.query(​'ul#people-list'​);
 
var​ last_person = list.query(​':last-child'​);
 
last_person.innerHtml;
 
// => 'Lucy'

Or we can chain it:

 
document.
 
query(​'ul#people-list'​).
 
query(​':last-child'​).
 
innerHtml;
 
// => 'Lucy'

The bottom line with finding elements on a page in Dart is that query and queryAll do pretty much what you expect, making it easy to query the DOM.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.170.223