-
Notifications
You must be signed in to change notification settings - Fork 0
/
链式方法
78 lines (51 loc) · 1.22 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
/*方法一、对象封装
var Base = {
getId : function (id) {
return document.getElementById(id)
},
getName : function (name) {
return document.getElementsByName(name)
},
getTagName : function (tag) {
return document.getElementsByTagName(tag);
}
};
*/
//方法二
var $ = function () {
return new Base();
}
function Base() {}
//创建一个数组,来保存获取的节点和节点数组
Base.prototype.elements = [];
//获取ID节点
Base.prototype.getId = function (id) {
this.elements.push(document.getElementById(id));
return this;
};
//获取元素节点
Base.prototype.getTagName = function (tag) {
var tags = document.getElementsByTagName(tag);
for (var i = 0; i < tags.length; i ++) {
this.elements.push(tags[i]);
}
return this;
};
Base.prototype.css = function (attr, value) {
for (var i = 0; i < this.elements.length; i ++) {
this.elements[i].style[attr] = value;
}
return this;
}
Base.prototype.html = function (str) {
for (var i = 0; i < this.elements.length; i ++) {
this.elements[i].innerHTML = str;
}
return this;
}
Base.prototype.click = function (fn) {
for (var i = 0; i < this.elements.length; i ++) {
this.elements[i].onclick = fn;
}
return this;
}