-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyteQuery.js
More file actions
62 lines (52 loc) · 1.35 KB
/
byteQuery.js
File metadata and controls
62 lines (52 loc) · 1.35 KB
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() {
var byteQuery = function(selector) {
if (!(this instanceof byteQuery)) {
return new byteQuery(selector);
}
if (typeof selector !== 'string') {
return this;
}
var nodes = document.querySelectorAll(selector);
nodes.forEach(function(value, index) {
this[index] = value;
}, this);
this.length = nodes.length;
return this;
}
byteQuery.prototype.each = function(callback, context) {
for (var i = 0; i < this.length; i++) {
callback.call(context || this, this[i], i, this);
}
return this;
}
byteQuery.prototype.attr = function(attribute, value) {
if (attribute === 'class') {
attribute = 'className';
}
if (this[0]) {
if (value) {
// Setting attribute
this[0][attribute] = value;
return this;
} else {
// Getting attribute
return this[0][attribute];
}
}
return undefined;
}
byteQuery.prototype.on = function(eventName, callback) {
this.each(function(target) {
target.addEventListener(eventName, callback, false);
});
return this;
}
byteQuery.prototype.off = function(eventName, callback) {
this.each(function(target) {
target.removeEventListener(eventName, callback, false);
});
return this;
}
window.byteQuery = byteQuery;
window.$ = byteQuery;
})()