-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJQuery.js
48 lines (40 loc) · 1.18 KB
/
JQuery.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
function YQueryElemnt(nodeList) {
this.nodeList = nodeList;
nodeList.forEach((now, index) => {
this[index] = now;
});
return this;
}
// constructor
function $(querySelectorStr){
const elements = document.querySelectorAll(querySelectorStr);
if(!elements) return null;
return new YQueryElemnt(elements);
}
// $ func
YQueryElemnt.prototype.css = function(attribute, value){
this.nodeList.forEach(element => {
element.style[attribute] = value;
});
return this;
}
// css
YQueryElemnt.prototype.html = function(html){
if(!html) return [...(this.nodeList)].map(element => element.innerHTML);
return this.nodeList.forEach(element => element.innerHTML = html);
}
//html
YQueryElemnt.prototype.on = function(eventType, callback){
this.nodeList.forEach(element => element["on" + eventType] = callback);
return this;
}
//on event
YQueryElemnt.prototype.getAttr = function(attrName){
return [...(this.nodeList)].map(element => element.getAttribute(attrName));
}
//getAttr
YQueryElemnt.prototype.setAttr = function(attrName, value){
this.nodeList.forEach(element => element[attrName] = value);
return this;
}
//setAttr