Methods
(inner) $(s, bopt) → {null|Element|Array.<(null|Element)>|Object.<(null|Element|Object)>}
Shortcut to querySelector
, but different if not given a string.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | Array.<string> | Object | Element |
|
||
b |
Element | Document |
<optional> |
document | If not having |
Returns:
-
if
s
is a string or Element- Type
- null | Element
-
if
s
is an Array- Type
- Array.<(null|Element)>
-
if
s
is an Object- Type
- Object.<(null|Element|Object)>
Examples
/// get the first button by a string
$("button, [type=button], [type=submit]");
/// assign elements by respective selectors
const [myForm, myTable, myTextArea] = $(["#myForm", ".myTable", "textarea"]);
/// safe to use in `Array.map()`
const [myForm, myTable, myTextArea] = ["#myForm", ".myTable", "textarea"].map($);
/// nested object
$({form: 'form', inputs: {text: '[type=text]', password: '[type=password]'}});
(inner) $$(s, baseopt) → {Array|Object}
Shortcut to querySelectorAll
but returns an array instead of NodeList
.
Different if not given a string, like $
differs from querySelector
;
however, for selectors have no matches, empty array is returned instead of null.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | Array.<string> | Object | one or more CSS selector string |
||
base |
Element | Document |
<optional> |
document | if not an object having |
Returns:
- Type
- Array | Object
Example
/// returns all trimmed values of <input>'s in `.myForm`.
$$(".myForm input").map(input => input.value.trim());
(inner) clearElement(elemopt) → {void}
call Element.replaceChildren()
without any argument specified.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
elem |
Element |
<optional> |
this |
- Deprecated:
- Yes
- Source:
- See:
Returns:
- Type
- void
(inner) createElement()
my old JSON format to represent DOM; shall be replaced by createElementFromJsonML
.
- Deprecated:
- Yes
- Source:
- See:
(inner) createElementFromJsonML(jsonml, namespaceopt) → {Element|TextNode}
Use JsonML to create an HTML element. For attributes setting, see setAttributes
.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
jsonml |
JsonML | ||
namespace |
string |
<optional> |
set this to use |
Returns:
- Type
- Element | TextNode
(inner) createElementFromTemplate(template) → {Node}
Use HTMLTemplateElement
to create an HTML element.
Parameters:
Name | Type | Description |
---|---|---|
template |
string | Node | HTMLTemplateElement |
|
Returns:
- Type
- Node
(inner) downloadData(data, filename, mimeTypeopt) → {string}
Download the given data.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
data |
string | ArrayBuffer | TypedArray | DataView | Blob | |||
filename |
string | |||
mimeType |
string |
<optional> |
'' |
Returns:
- Type
- string
(inner) downloadURL(href, filename) → {undefined}
Make the borowser download the given URL to the local filesystem.
Parameters:
Name | Type | Description |
---|---|---|
href |
URL | string | |
filename |
string |
Returns:
- Type
- undefined
(inner) extendElementPrototype()
Add some methods to Element
class.
(inner) getNodes(acceptopt, rejectopt, baseopt) → {Array.<Node>}
Get nodes within the specified node.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
accept |
string | Array.<string> | function |
<optional> |
()=>true | fit nodes are included |
reject |
string | Array.<string> | function |
<optional> |
()=>false | fit nodes are excluded |
base |
Element | Document |
<optional> |
document | root of the DOM tree to travere |
Returns:
nodes which fit accept
but not within those fit reject
- Type
- Array.<Node>
(inner) isEventInElement(event, elemopt) → {boolean}
Check wheather a mouse event happens inside an element, even its target is not the element.
Could be assign to Element.prototype
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
event |
MouseEvent | |||
elem |
Element | string |
<optional> |
this | the Element or the query selector to it |
Returns:
- Type
- boolean
(inner) parseHTML(html, selectorsopt) → {HTMLDocument|Element|null}
Shortcut to DOMParser.parseFromString
but returns the first element in HTMLBodyElement
by default.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
html |
string | |||
selectors |
* |
<optional> |
body > * | If given but not a string or array of strings, the whole |
Returns:
Incomplete HTML string may lead to unexpected result.
Browsers may unexpectedly add essential tags such as <html>
, <head>
, and <body>
,
even given html
string contains no such tags.
It may also omit tags if the structure is not complete.
For example, <tr>
as the root node of the html
string
may cause browsers not creating elements but only the text nodes within them.
- Type
- HTMLDocument | Element | null
Examples
/// returns an `Element` whose `tagName` is "em" and has string "hi!" as its text content.
parseHTML("<EM>hi!</em>");
/// returns an `HTMLDocument` which could be represented by "<html><head></head><body><em>HI</em></body></html>".
parseHTML("<EM>hi!</em>", null);
/// returns an `HTMLDocument` which could be represented by "<html><head><title>title</title></head><body><p>paragraph</p></body></html>".
parseHTML("<title>title</title><p>paragraph</p>", {});
/// returns null because `<title>` is automatically inserted into `<head>` and thus nothing in `<body>`.
parseHTML("<title>my title</title>");
/// returns null because `<tr>` is not allowed to exist outsied `<table>` and thus there is only a text node (which isn't an `Element`) in `<body>`.
parseHTML("<tr><td>QQ</td></tr>");
(inner) setAria(s, attr, valueopt) → {null|Element}
Set ARIA attribute of the first Element, if found any.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | Element | CSS selector string, or an Element |
||
attr |
string | ARIA attribute name, |
||
value |
NullLike | true | string |
<optional> |
null | NullLike value removes the attribute; true set it as empty string; others treated as string value. |
Returns:
the first found Element, or null.
- Type
- null | Element
(inner) setAttributes(s, attributes) → {null|Element}
Set attributes of an Element by an object as a map.
Attributes with namespace prefix are also supported.
Attributes with undefined, null, or false value in the first argument would be removed in the Element, except event listeners.
Event listeners are added by "on*"" such as "onclick" (case-insensitive).
CSS class could be assigned by string or Array.<string>
.
Inline style could be assigned by string or Object.<string>
.
Set text
property of the first argument would assign the element's textContent
, though it's not an attribute.
Parameters:
Name | Type | Description | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
s |
string | Element | CSS selector string, or an Element |
||||||||||||||||||||||||
attributes |
Object |
Properties
|
Returns:
the first found Element, or null if not found.
- Type
- null | Element
Examples
/// set body background to red
setAttributes('#some-input', {
type: 'text',
value: 'my string'
});
/// set body background to red and text to white
setAttributes('body', {style: 'background-color: red; color: white'});
/// set body background to red
setAttributes('body', {style: {
backgroundColor: 'red',
color: 'white'
}});
/// set body class
setAttributes('body', {class: 'my-class your-class'});
/// set body class
setAttributes('body', {class: ['my-class', 'your-class']});
(inner) setText(s, textopt) → {null|Element}
Set textContent
of the first Element, if found any. Useful if you are not sure about its existence.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
s |
string | Element | CSS selector string, or an Element |
||
text |
string |
<optional> |
'' |
Returns:
the first found Element, or null if not found.
- Type
- null | Element
Type Definitions
JsonML
JSON Markup Language structure
Type:
- Array
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
0 |
string | HTML tag to create |
|
1 |
Object | attributes those to be set to the HTML element |
|
2 |
JsonML | string |
<optional> |
first child of the element created |
3 |
JsonML | string |
<optional> |
second child of the element created |
- Source:
- See:
NullLike
Type:
- undefined | null | false