I'm starting with some of the Dart examples. Then I wanted to query the DOM with document.query('#someId')
as described here, but it seems there was no query method in document. Also creating a new element by `new Element.tag('p') doesn't work.
Then I figure out that it will work, when I change the imported package from dart:dom
to dart:html
. But using both of them gives me a bunch of duplicate definition of _XYZ
.
So I wonder:
dart:html
and dart:dom
packageThe dart:html library provides the querySelector function to search for elements in the DOM. Element querySelector(String selectors); The querySelector() function returns the first element that matches the specified group of selectors. "
Answering a bit out of order
Which one should I use: you should use dart:html
it provides the cleanest abstraction on top of the DOM.
why cant I use both: it should strictly not be needed, but you can actually get to the underlying dart:dom
implementations from dart:html
using a dirty hack described here. The better and more clean solution is to use Dart's ability to rename imports i.e. #import('dart:dom', prefix: 'dom');
as described by @munificent below.
whats the different between dart:html and dart:dom package. I tend to think of the difference between them as similar to JQuery (dart:html
) vs JS DOM manipulation (dart:dom
).
The Dart team is trying hard to make the dart:html
API as easy and unsurprising (in the good way) to use as we have gotten used to from libraries such as JQuery (dom manipulation), Tree.js (WebGL programming) and D3 (SVG drawing). Further they also try to follow one API style across all these areas of functionality so that the SVG or WebGL API use similar constructs as the DOM API, thus ensuring that all the parts play nicely together.
Update: as of may 2012 dart:dom is now deprecated and will be removed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With