the $query
(aliased with $q
) object exposes two methods: first()
and all()
. Both take a selector string as the first argument and, optionally, a context element as the second.
$q.first(".foo"); // => first element of class `foo`
$q.all(".foo"); // => array (yes, not NodeList) of all elements of class `foo`
var elem = $q.first("p");
$q.first(".foo", elem ); // => first child of `elem` of class `foo`
$q.all(".foo", elem ); // => all children of `elem` of class `foo`
You can also build the query in advance and evaluate it lazily when you need:
var query = $q(".foo .bar"); // build the query
query.first() // => first element matching the selector, or null
query.all() // => all elements matching the selector
Maybe you want to add this to http://microjs.com/#css :o)