自定义类StyleSheet跨浏览器操作样式表中的规则
这是群里网友地瓜提供的一个类,不熟悉样式表对象和样式规则对象的浏览器差异的可以看看
/**
* Stylesheet.js: utility methods for scripting CSS stylesheets.
*
* This module defines a Stylesheet class that is a simple wrapper
* around an element of the document.styleSheets[] array. It defines useful
* cross-platform methods for querying and modifying the stylesheet.
**/ // Construct a new Stylesheet object that wraps the specified CSSStylesheet.
// If ss is a number, look up the stylesheet in the styleSheet[] array.
function Stylesheet(ss) {
//~~~document.styleSheets
if (typeof ss == "number") ss = document.styleSheets[ss];
this.ss = ss;
} // Return the rules array for this stylesheet.
Stylesheet.prototype.getRules = function() {
// Use the W3C property if defined; otherwise use the IE property
//~~~stylesheet.cssRules(w3c) , stylesheet.rules(ie)
return this.ss.cssRules?this.ss.cssRules:this.ss.rules;
} // Return a rule of the stylesheet. If s is a number, we return the rule
// at that index. Otherwise, we assume s is a selector and look for a rule
// that matches that selector.
Stylesheet.prototype.getRule = function(s) {
var rules = this.getRules();
if (!rules) return null;
if (typeof s == "number") return rules[s];
// Assume s is a selector
// Loop backward through the rules so that if there is more than one
// rule that matches s, we find the one with the highest precedence.
s = s.toLowerCase();
for(var i = rules.length-1; i >= 0; i--) {
//~~~~rule.selectorText
if (rules[i].selectorText.toLowerCase() == s) return rules[i];
}
return null;
}; // Return the CSS2Properties object for the specified rule.
// Rules can be specified by number or by selector.
Stylesheet.prototype.getStyles = function(s) {
var rule = this.getRule(s);
//~~~rule.style(like the element.style, is a styleDeclearation)
if (rule && rule.style) return rule.style;
else return null;
}; // Return the style text for the specified rule.
Stylesheet.prototype.getStyleText = function(s) {
var rule = this.getRule(s);
//~~~rule.style.cssText(just like element.style.cssText)
if (rule && rule.style && rule.style.cssText) return rule.style.cssText;
else return "";
}; // Insert a rule into the stylesheet.
// The rule consists of the specified selector and style strings.
// It is inserted at index n. If n is omitted, it is appended to the end.
Stylesheet.prototype.insertRule = function(selector, styles, n) {
if (n == undefined) {
var rules = this.getRules();
n = rules.length;
}
if (this.ss.insertRule) // Try the W3C API first
//~~~stylesheet.insertRule(ruleText,index) w3c,
//~~~stylesheet.addRule(selector,cssText,index) ie
this.ss.insertRule(selector + "{" + styles + "}", n);
else if (this.ss.addRule) // Otherwise use the IE API
this.ss.addRule(selector, styles, n);
}; // Remove the rule from the specified position in the stylesheet.
// If s is a number, delete the rule at that position.
// If s is a string, delete the rule with that selector.
// If s is not specified, delete the last rule in the stylesheet.
Stylesheet.prototype.deleteRule = function(s) {
// If s is undefined, make it the index of the last rule
if (s == undefined) {
var rules = this.getRules();
s = rules.length-1;
} // If s is not a number, look for a matching rule and get its index.
if (typeof s != "number") {
s = s.toLowerCase(); // convert to lowercase
var rules = this.getRules();
for(var i = rules.length-1; i >= 0; i--) {
if (rules[i].selectorText.toLowerCase() == s) {
s = i; // Remember the index of the rule to delete
break; // And stop searching
}
} // If we didn't find a match, just give up.
if (i == -1) return;
} // At this point, s will be a number.
// Try the W3C API first, then try the IE API
//~~~stylesheet.deleteRule(index) w3c
//~~~stylesheet.removeRule(index) ie
if (this.ss.deleteRule) this.ss.deleteRule(s);
else if (this.ss.removeRule) this.ss.removeRule(s);
};
自定义类StyleSheet跨浏览器操作样式表中的规则的更多相关文章
- DOM操作样式表及其兼容性
DOM操作样式表的时候,存在很多浏览器兼容上的问题,测试的时候用的是Firefox 28.0.IE11.IE8.Chrome.测试的时候发现,不兼容问题基本上都是IE8和非IE浏览器之家的问题,很多I ...
- Javascript高级编程学习笔记(51)—— DOM2和DOM3(3)操作样式表
操作样式表 在JS中样式表用一种类型来表示,以便我们在JS对其进行操作 这一类型就是CSSStyleSheet 即CSS样式表类型,包括了之前 style 对象所不包括的外部样式表以及嵌入样式表 其中 ...
- JS:操作样式表3:内联和外链样式
var box = document.getElementById("box"); box.style.属性;只能读取修改行内样式. //访问元素样式2,对外链样式表进行操作 do ...
- JavaScript编程:使用DOM操作样式表
6.使用DOM操作样式表: 操纵元素的Style样式属性: background-color:style.backgroundColor color:style.col ...
- CSS中的层叠、特殊性、继承、样式表中的@import
CSS中的层叠.特殊性.继承.样式表中的@import 层叠 CSS有一个机制是层叠,层叠可以理解为对样式的覆盖,优先性为: 网站开发者的样式表 用户样式(通过设置浏览器的显示选项) 浏览器默认的样式 ...
- css样式表中的样式覆盖顺序
刚才写zenktodo的时候,通过动态添加class的方式修改一个div的样式,总是不起作用. #navigator { height: 100%; width: 200; position: abs ...
- css样式表中的样式覆盖顺序(转)
有时候在写CSS的过程中,某些限制总是不起作用,这就涉及了CSS样式覆盖的问题,如下 Css代码 #navigator { height: 100%; width: 200; position: ...
- css样式表中四种属性选择器
学习此连接的总结http://developer.51cto.com/art/201009/226158.htmcss样式表中四种属性选择器1> 简易属性 tag[class]{ font-we ...
- 一个DOM元素同时拥有多个类名时的样式产生冲突时 属性取决于css样式表中后读取到的属性
如果一个DOM元素包含多个类名,其中的两个类名的属性产生冲突,并不是根据htnl中类名的顺序来决定DOM元素的属性, 而是根据css样式中的顺序来决定DOM元素的属性,它取决于css样式表中后读取到的 ...
随机推荐
- C、C++中引用与指针的区别
1:引用的和指针在概念上的区别 引用是变量的别名,例如 int m; int &n=m; 引用作为一个别名.它在逻辑上不是独立的,它的存在具有依附性,所以引用必须在一开始就被初始化,而且其引用 ...
- pojAGTC(LCS,DP)
题目链接: 啊哈哈,点我点我 题意:给两个字符串,找出经过多少个操作能够使得两个串相等.. 思路:找出两个串的最长公共子序列,然后用最大的串的长度减去最长公共子序列的长度得到的就是须要的操作数.. 题 ...
- zoj 1081 (改进的弧长算法)(转)
看到网上除了射线法,很长一段代码之外,看到了一个很简单的算法解决这个问题,特意转了过来 /* 这个算法是源自<计算机图形学基础教程>(孙家广,清华大学出版社),在该书 的48-49页,名字 ...
- SQL Server 2012学习笔记 1 命令行安装
setup.exe /Q /IACCEPTSQLSERVERLICENSETERMS /ACTION=install /PID=748RB-X4T6B-MRM7V-RTVFF-CHC8H /FEATU ...
- Java关键字static
链接地址:http://www.cnblogs.com/devinzhang/archive/2011/12/13/2286367.html static表示“全局”或者“静态”的意思,用来修饰成员变 ...
- HOOK API (一)——HOOK基础+一个鼠标钩子实例
HOOK API (一)——HOOK基础+一个鼠标钩子实例 0x00 起因 最近在做毕业设计,有一个功能是需要实现对剪切板的监控和进程的防终止保护.原本想从内核层实现,但没有头绪.最后决定从调用层入手 ...
- URL地址的编码和解码问题
编码:encodeURIComponent() 方法:把URI字符串采用 UTF-8编码格式转化成escape格式的字符串.与encodeURI()相比,这个方法将对更多的字符进行编码,比如 / 等字 ...
- bzoj 1047 : [HAOI2007]理想的正方形 单调队列dp
题目链接 1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2369 Solved: 1266[Submi ...
- excel读入数据库
POI3.9效率大幅度提高,支持xls以及xlsx. 首先需要POI的JAR包,MAVEN配置如下: <!-- excel start --> <dependency> < ...
- 【图文教程】用“iz3d”软件将您的游戏打造为红蓝3D游戏。
iz3d是一款能将普通3D游戏转换为红蓝3D游戏的软件.基本上支持所有游戏,或许没用过的人会认为这只是类似于播放器中的一个小功能,将平面图形做成“伪3D”红蓝效果. 实际上不是的,游戏与平面图的结构不 ...