-
Notifications
You must be signed in to change notification settings - Fork 0
/
添加移除外部的css样式
36 lines (32 loc) · 1.04 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
//前台调用
var $ = function (_this) {
return new Base(_this);
}
//基础库
function Base(_this) {
//创建一个数组,来保存获取的节点和节点数组
this.elements = [];
if (_this != undefined) { //_this是一个对象,undefined也是一个对象,区别与typeof返回的带单引号的'undefined'
this.elements[0] = _this;
}
}
//添加link或style的CSS规则
Base.prototype.addRule = function (num, selectorText, cssText, position) {
var sheet = document.styleSheets[num];
if (typeof sheet.insertRule != 'undefined') {//W3C
sheet.insertRule(selectorText + '{' + cssText + '}', position);
} else if (typeof sheet.addRule != 'undefined') {//IE
sheet.addRule(selectorText, cssText, position);
}
return this;
}
//移除link或style的CSS规则
Base.prototype.removeRule = function (num, index) {
var sheet = document.styleSheets[num];
if (typeof sheet.deleteRule != 'undefined') {//W3C
sheet.deleteRule(index);
} else if (typeof sheet.removeRule != 'undefined') {//IE
sheet.removeRule(index);
}
return this;
}