-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdom.js
78 lines (67 loc) · 1.7 KB
/
dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const $ = function (selector) {
const nodeList = document.querySelectorAll(selector);
const text = function(content){
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].innerText = content;
}
}
const html = function(content){
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].innerHTML = content;
}
}
const addClass = function(className){
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].classList.add(className);
}
}
const removeClass = function(className){
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].classList.remove(className);
}
}
const toggleClass = function(className){
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].classList.toggle(className);
}
}
const empty = function(){
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].innerHTML = '';
}
}
const append = function(content){
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].innerHTML += content;
}
}
const prepend = function(content){
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].innerHTML = content + nodeList[i].innerHTML;
}
}
const val = function (content) {
if(content === undefined){
return nodeList[0].value;
} else {
nodeList[0].value = content;
}
}
const on = function (action, cb) {
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].addEventListener(action, cb);
}
}
return {
text: text,
html: html,
addClass: addClass,
removeClass: removeClass,
toggleClass: toggleClass,
empty: empty,
append: append,
prepend: prepend,
on: on,
val: val
};
}