new $Document()
Used to represent a document in better-dom
Extends
Methods
-
clone(deepCopy)
-
Clone element
Parameters:
Name Type Description deepCopyBoolean truewhen all children should also be cloned, otherwisefalse- Inherited From:
Returns:
A clone of the current element
- Type
- $Node
Example
ul.clone(true); // => clone of <ul> with all it's children ul.clone(false); // => clone of <ul> element ONLY
-
contains(element)
-
Check if an element is inside of the current context
Parameters:
Name Type Description element$Node Element to check
- Inherited From:
Returns:
trueif success andfalseotherwise- Type
- Boolean
Example
DOM.contains(DOM.find("body")); // => true DOM.find("body").contains(DOM); // => false -
create(content)
-
Create a new $Element from a HTML string
Parameters:
Name Type Description contentString HTMLString
Returns:
an element wrapper
- Type
- $Element
Example
DOM.create("<div>"); // => wrapper of <div> DOM.create("<a><span></span></a>"); // => wrapper of <a> + innner <span> -
createAll(content)
-
Create a new Array.<$Element> from a HTML string
Parameters:
Name Type Description contentString HTMLString
Returns:
an array of element wrappers
- Type
- Array.<$Element>
Example
DOM.createAll("<span></span><b></b>"); // => array with 2 $Elements: <span> and <b> -
extend(selector, definition)
-
Declare a live extension
Parameters:
Name Type Description selectorString css selector of which elements to capture
definitionObject live extension definition
Example
DOM.extend("selector", { constructor: function() { // initialize component }, publicMethod: function() { // ... } }); -
find(selector)
-
Find the first matched element in the current context by a CSS selector
Parameters:
Name Type Description selectorString CSS selector
- Inherited From:
Returns:
The first matched element
- Type
- $Element
Example
var body = DOM.find("body"); // => <body> wrapper var foo = body.find(".foo"); // => the first element with class "foo" foo.find(".bar>span"); // => the first element that matches ".bar>span" -
findAll(selector)
-
Find all matched elements in the current context by a CSS selector
Parameters:
Name Type Description selectorString CSS selector
- Inherited From:
Returns:
An array of matched elements
- Type
- Array.<$Element>
Example
DOM.findAll("a"); // => all links in the document context.findAll("ol>li"); // => all <li> inside of <ol> -
fire(type [, detail])
-
Trigger an event of specific type with optional data
Parameters:
Name Type Argument Description typeString Type of event
detailObject <optional>
Custom event data
- Inherited From:
Returns:
trueif default event action was NOT prevented- Type
- Boolean
Example
link.fire("click"); // fire click event link.fire("my:event", {a: "b"}); // fire "my:event" with custom data -
get(name [, defaultValue])
-
Get property or attribute value by name
Parameters:
Name Type Argument Description nameString | Array Property or attribute name or array of names
defaultValueObject <optional>
Default value if returned is
null- Inherited From:
Returns:
Value of property or attribute
- Type
- Object
Example
link.get("title"); // => property title link.get("data-custom"); // => custom attribute data-custom -
importScripts(url [, callback])
-
Import external scripts on the page and call optional callback when it will be done
Parameters:
Name Type Argument Description urlString <repeatable>
script file url(s)
callbackfunction <optional>
callback that is triggered when all scripts are loaded
Example
DOM.importScripts("http://cdn/script1.js", function() { // do something when the script is loaded }); // loading several scripts sequentially DOM.importScripts("http://cdn/script2.js", "http://cdn/script3.js"); -
importStyles(selector, cssText)
-
Append global css styles
Parameters:
Name Type Description selectorString css selector
cssTextString css rules
Example
DOM.importStyles(".foo, .bar", "background: white; color: gray"); // more complex selectors DOM.importStyles("@keyframes fade", "from {opacity: 0.99} to {opacity: 1}"); -
mock(content [, varMap])
-
Return $Element initialized with all existing live extensions. Also exposes private functions that do not usually exist. Accepts the same arguments as DOM.create
Parameters:
Name Type Argument Description contentString HTMLString
varMapObject | Array <optional>
key/value map of variables
- See:
Returns:
a mocked instance
- Type
- $Element
-
on(type [, options] [, args], callback)
-
Bind a DOM event listener
Parameters:
Name Type Argument Description typeString Event type
optionsObject | String <optional>
Event options object or css selector to match on
argsArray <optional>
Array of handler arguments to pass into the callback
callbackfunction Event listener callback
- Inherited From:
Returns:
Functor to cancel the listener
- Type
- function
Example
link.on("focus", function() { // do something on focus }); link.on("click", "i", function() { // do something on internal <i> click }); link.on("click", "span", ["currentTarget"], function(span) { // <span> is the element was clicked }); link.on("blur", {once: true}, function() { // event fired only once }); link.on("mousedown", {capture: true}, function() { // event fired on capturing phase }); -
set(name, value)
-
Set property/attribute value by name
Parameters:
Name Type Description nameString | Object | Array | function property/attribute name
valueString | function property/attribute value or functor
- Inherited From:
Returns:
Self
- Type
- $Node
Example
link.set("title", "mytitle"); // set title property link.set("data-custom", "foo"); // set custom attribute data-custom