domquery 0.1.2
domquery: ^0.1.2 copied to clipboard
An abstraction layer of the DOM implementation that allows us to manipulate and traverse documents in an easy and intuitive way. This library is not intended as a replacement for the DOM implementatio [...]
domQuery #
An abstraction of the DOM implementation that allows us to manipulate and traverse documents in an easy and intuitive way. This library is not intended as a replacement for the DOM implementation. But in most cases you will not need to use native DOM functions.
DomNode methods #
node.query(String selectors): Finds elementsnode.attr(String name, [Object value]): gets or sets attributesnode.text([Object value]): gets or sets inner textnode.html([Object value]): gets or sets inner htmlnode.data(String name, [Object value]): gets or sets datanode.append(Object obj): appends an objectnode.prepend(Object obj): prepends an objectnode.name(): gets the node namenode.parent(): gets the parent elementnode.clean(): removes all child nodesnode.remove(): removes the node from the documentnode.element(): gets the nativeElementobject
Examples #
Creating instances #
import 'package:domquery/dom/node.dart';
void main() {
var node;
// creates a node with attributes
node = new DomNode('item', attributes: {'id': 101, 'title': 'Item title'});
print(node);
// creates a node with inner text
node = new DomNode('item', text: 'Lorem ipsum ...');
print(node);
// creates a node with inner html
node = new DomNode('item', html: '<span>Lorem ipsum ...</span>');
print(node);
// creates a node with attributes and child nodes
node = new DomNode('item', attributes: {'id': 101, 'title': 'Item title'}, callback: (DomNode target) {
target.append('<node id="1" />');
target.append('<node id="2" />');
target.append('<node id="3" />');
});
print(node);
}
Traversing documents #
You can use the same function domQuery to retrieve single or multiple elements.
import 'package:domquery/utils.dart';
void main() {
// accessing a single element
var title = domQuery('h1');
title.text('Changing title');
// accessing multiple elements
var paragraphs = domQuery('p');
paragraphs.forEach((item) {
item.html('<strong>Paragraph</strong>');
});
}