-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.js
62 lines (54 loc) · 1.48 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
var DOM = {};
DOM._element = function(name) {
var elem = document.createElement(name);
for (var i = 1, n = arguments.length; i < n; ++i) {
var thisOne = arguments[i];
if (typeof thisOne == 'undefined') {
// ignore undefined node
} else if (typeof thisOne == 'string') {
elem.appendChild(document.createTextNode(thisOne));
} else if (null != thisOne.nodeName) {
elem.appendChild(thisOne);
} else if (typeof thisOne == 'object') {
for (var x in thisOne) {
if (thisOne.hasOwnProperty(x)) {
if (typeof thisOne[x] == 'function' && 0 == x.indexOf('on')) {
//elem[x] = thisOne[x];
elem.addEventListener(x.substring(2), thisOne[x], false);
} else {
elem.setAttribute(x, thisOne[x]);
}
}
}
}
}
return elem;
};
var tags = "div span input style script link button form input textarea button h1 h2 h3 h4".split(' ');
for (var i = 0, n = tags.length; i < n; ++i) {
var thisOne = tags[i];
DOM[thisOne] = DOM._element.bind(DOM, thisOne);
}
DOM.append = function (to, elem) {
to.appendChild(elem);
};
DOM.appendToBody = function(elem) {
DOM.append(document.body, elem);
};
DOM.appendToHead = function(elem) {
DOM.append(document.head, elem);
};
DOM.replace = function (to, elem) {
if (null != to && null != elem) {
to.parentNode.replaceChild(elem, to);
}
};
DOM.remove = function (elem) {
if (null != elem) {
elem.parentNode.removeChild(elem);
}
};
DOM.byId = function(id) {
return document.getElementById(id);
};
module.exports = DOM;