-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcjQuery.js
More file actions
168 lines (166 loc) · 5.34 KB
/
cjQuery.js
File metadata and controls
168 lines (166 loc) · 5.34 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* cjQuery Beta v0.1.0
* This is javascript library that help using basic javascript easily
* Author: inf0craw1
* Date: 2021-02-02
*/
function cjQuery(s){
function isSelectorExist(selector){
if(selector[0] != undefined){
return true;
}
return false;
}
function validateSelector(selector){
if(!isSelectorExist(selector)){
console.warn(`Element "${s}" is not exist`); return false;
}
return true;
}
cjQuery.prototype = {
selector: (typeof s === "string") ? document.querySelectorAll(s) : [s],
selectString: s,
color: function(c){
if(!validateSelector(this.selector)) return false;
for(elem of this.selector){
elem.style.color = c;
}
return this;
},
text: function(t){
if(!validateSelector(this.selector)) return false;
if(t == null){
if(this.selector.length > 1){
let res = [];
for(elem of this.selector){
res.push(elem.innerHTML);
}
return res;
}
return this.selector[0].innerHTML;
}
for(elem of this.selector){
elem.innerHTML = t;
}
return this;
},
hide: function(){
if(!validateSelector(this.selector)) return false;
for(elem of this.selector){
elem.style.display = "none";
}
return this;
},
show: function(){
if(!validateSelector(this.selector)) return false;
for(elem of this.selector){
elem.style.display = "block";
}
return this;
},
value: function(v){
if(!validateSelector(this.selector)) return false;
if(v == null){
if(this.selector.length > 1){
let res = [];
for(elem of this.selector){
res.push(elem.getAttribute("value"));
}
return res;
}
return this.selector[0].getAttribute("value");
}
for(elem of this.selector){
elem.setAttribute("value", v);
}
return this;
},
css: function(prop, propValue){
if(!validateSelector(this.selector)) return false;
if(propValue == null){
if(this.selector.length > 1){
let res = [];
for(elem of this.selector){
res.push(elem.style[prop]);
}
return res;
}
return this.selector[0].style[prop];
}
for(elem of this.selector){
elem.style[prop] = propValue;
}
return this;
},
on: function(e, f){
if(!validateSelector(this.selector)) return false;
for(elem of this.selector){
elem.addEventListener(e, f);
}
return this;
},
attr: function(attrName, attrValue){
if(!validateSelector(this.selector)) return false;
if(attrValue == null){
if(this.selector.length > 1){
let res = [];
for(elem of this.selector){
res.push(elem.getAttribute(attrName));
}
return res;
}
return this.selector[0].getAttribute(attrName);
}
for(elem of this.selector){
elem.setAttribute(attrName, attrValue);
}
return this;
},
parent: function(){
if(!validateSelector(this.selector)) return false;
this.selector = this.selector[0].parentNode;
return this;
},
addClass: function(c) {
if(!validateSelector(this.selector)) return false;
for(elem of this.selector){
elem.classList.add(c);
}
return this;
},
removeClass: function(c){
if(!validateSelector(this.selector)) return false;
for(elem of this.selector){
elem.classList.remove(c);
}
return this;
},
toggleClass: function(c){
if(!validateSelector(this.selector)) return false;
for(elem of this.selector){
elem.classList.toggle(c);
}
return this;
},
hasClass: function(c){
if(!validateSelector(this.selector)) return false;
if(this.selector.length > 1){
let res = [];
for(elem of this.selector){
res.push(elem.classList.contains(c));
}
return res;
}
return this.selector[0].classList.contains(c);
},
replaceClass: function(targetC, c){
if(!validateSelector(this.selector)) return false;
for(elem of this.selector){
elem.classList.replace(targetC, c);
}
return this;
}
}
return cjQuery.prototype;
}
window.$ = cjQuery;