-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkew.js
62 lines (49 loc) · 1.74 KB
/
kew.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
(function (window) {
var $kew = function (selector, context) {
this.elements = (context || document).querySelectorAll(selector);
this.length = this.elements.length;
return this;
};
$kew.prototype = {
ready: function (callback) {
document.addEventListener("DOMContentLoaded", callback);
},
each: function (callback) {
for (var i = 0; i < this.length; i++) {
callback(this.elements[i], i, this.elements);
}
return this;
},
attr: function (attrName, attrValue) {
if (typeof attrName === 'string' && typeof attrValue === 'string') {
for (var i = 0; i < this.length; i++) {
var attr = '';
if (this.elements[i].getAttribute(attrName) !== 'undefined') {
attr = this.elements[i].getAttribute(attrName);
}
this.elements[i].setAttribute(attrName, attr + ' ' + attrValue);
}
}
},
addClass: function (className) {
this.each(function (element) {
element.classList.add(className);
});
return this;
},
removeClass: function (className) {
this.each(function (element) {
element.classList.remove(className);
});
return this;
},
removeElements: function () {
for (var i = 0; i < this.length; i++) {
this.elements[i].parentElement.removeChild(this.elements[i]);
}
}
};
window.$kew = function (selector, context) {
return new $kew(selector, context);
};
})(window);