-
Notifications
You must be signed in to change notification settings - Fork 0
/
minijq.js
53 lines (43 loc) · 1.11 KB
/
minijq.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
// Re-implementation of jQuery.hasClass
HTMLElement.prototype.hasClass = function(klass) {
var el = this,
classes = el.className.split(' ');
return classes.indexOf(klass) >= 0;
}
// Re-implementation of jQuery.addClass
HTMLElement.prototype.addClass = function(klass) {
var el = this,
classes = el.className.split(' ');
if(!el.hasClass(klass)) {
classes.push(klass);
el.className = classes.join(' ');
}
return el;
}
// Re-implementation of jQuery.removeClass
HTMLElement.prototype.removeClass = function(klass) {
var el = this,
classes = el.className.split(' '),
idx;
while(classes.indexOf(klass) >= 0) {
idx = classes.indexOf(klass);
classes.splice(idx, 1);
}
el.className = classes.join(' ');
return el;
}
// Re-implementation of jQuery.toggleClass
HTMLElement.prototype.toggleClass = function(klass) {
if(this.hasClass(klass)) {
this.removeClass(klass);
}
else {
this.addClass(klass);
}
return this;
}
// Re-implementation of jQuery.on
HTMLElement.prototype.on = function(evtName, callback) {
this.addEventListener(evtName, callback);
return this;
}