-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path设置CSS
50 lines (45 loc) · 1.2 KB
/
设置CSS
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
//前台调用
var $ = function () {
return new Base();
}
//基础库
function Base() {}
//创建一个数组,来保存获取的节点和节点数组
Base.prototype.elements = [];
//获取CLASS节点数组
Base.prototype.getClass = function (className, idName) {
var node = null;
if (arguments.length == 2) {
node = document.getElementById(idName);
} else {
node = document;
}
var all = node.getElementsByTagName('*');
for (var i = 0; i < all.length; i ++) {
if (all[i].className == className) {
this.elements.push(all[i]);
}
}
return this;
}
//获取某一个节点
Base.prototype.getElement = function (num) {
var element = this.elements[num];
this.elements = [];
this.elements[0] = element;
return this;
};
//设置CSS
Base.prototype.css = function (attr, value) {
for (var i = 0; i < this.elements.length; i ++) {
if (arguments.length == 1) {//获取属性
if (typeof window.getComputedStyle != 'undefined') {//W3C
return window.getComputedStyle(this.elements[i], null)[attr];
} else if (typeof this.elements[i].currentStyle != 'undeinfed') {//IE
return this.elements[i].currentStyle[attr];
}
}
this.elements[i].style[attr] = value;//设置属性
}
return this;
}