1 | ## dom-tree
|
2 |
|
3 | Library to manipulate DOM elements
|
4 |
|
5 | Example:
|
6 |
|
7 | ```js
|
8 | var dom = require('dom-tree')
|
9 |
|
10 | dom.add(document.body, '<h1>{title}</h1>{content}', {
|
11 | title: 'Hello',
|
12 | content: 'Welcome!'
|
13 | })
|
14 | ```
|
15 |
|
16 | ## Install
|
17 |
|
18 | ```bash
|
19 | $ npm install dom-tree
|
20 | ```
|
21 |
|
22 | ## API
|
23 |
|
24 | ### add(element, child)
|
25 |
|
26 | Adds `child` to `el`
|
27 |
|
28 | ```js
|
29 | add(document.body, document.createElement('textarea'))
|
30 | add('body .content', document.createElement('textarea'))
|
31 | add('.content', '<div>hello</div>')
|
32 | add('.content', '<h1>{title}</h1>', { title: 'Hello!' })
|
33 | ```
|
34 |
|
35 | ### addAfter(parent, child, reference)
|
36 |
|
37 | Similar to `addBefore`
|
38 |
|
39 | ### addBefore(parent, child, reference)
|
40 |
|
41 | ```js
|
42 | addBefore(document.body, document.createElement('textarea'), document.body.firstChild)
|
43 | addBefore('body', '<h1>{msg}</h1>', { msg: 'foobar' }, document.body.firstChild)
|
44 | ```
|
45 |
|
46 | ### insert(element, parent)
|
47 |
|
48 | insert `element` to `parent` as child
|
49 |
|
50 | ```js
|
51 | insert(document.createElement('textarea'), document.body)
|
52 | insert('<input />', '.content')
|
53 | insert('<h1>{title}</h1>', { title: 'hello' }, '.content')
|
54 | ```
|
55 |
|
56 | ### replace(parent, target, replacement)
|
57 |
|
58 | replace `target` with `replacement`
|
59 |
|
60 | ```js
|
61 | replace(document.body, document.body.firstChild, document.createElement('textarea'))
|
62 | replace('body .content', '.content ul', '<h1>hello</h1>')
|
63 | replace('body .content', '.content ul', '<h1>{msg}</h1>', { msg: 'hello!' })
|
64 | ```
|
65 |
|
66 | ### remove(element)
|
67 |
|
68 | remove `element`
|
69 |
|
70 | ```js
|
71 | remove(document.body.firstChild)
|
72 | remove('body .content')
|
73 | ```
|
74 |
|
75 | ### remove(parent, child)
|
76 |
|
77 | remove `child`
|
78 |
|
79 | ```js
|
80 | remove(document.body.firstChild, 'h1')
|
81 | ```
|