这是群里网友地瓜提供的一个类,不熟悉样式表对象和样式规则对象的浏览器差异的可以看看

/**
* 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跨浏览器操作样式表中的规则的更多相关文章

  1. DOM操作样式表及其兼容性

    DOM操作样式表的时候,存在很多浏览器兼容上的问题,测试的时候用的是Firefox 28.0.IE11.IE8.Chrome.测试的时候发现,不兼容问题基本上都是IE8和非IE浏览器之家的问题,很多I ...

  2. Javascript高级编程学习笔记(51)—— DOM2和DOM3(3)操作样式表

    操作样式表 在JS中样式表用一种类型来表示,以便我们在JS对其进行操作 这一类型就是CSSStyleSheet 即CSS样式表类型,包括了之前 style 对象所不包括的外部样式表以及嵌入样式表 其中 ...

  3. JS:操作样式表3:内联和外链样式

    var box = document.getElementById("box"); box.style.属性;只能读取修改行内样式. //访问元素样式2,对外链样式表进行操作 do ...

  4. JavaScript编程:使用DOM操作样式表

    6.使用DOM操作样式表: 操纵元素的Style样式属性:         background-color:style.backgroundColor         color:style.col ...

  5. CSS中的层叠、特殊性、继承、样式表中的@import

    CSS中的层叠.特殊性.继承.样式表中的@import 层叠 CSS有一个机制是层叠,层叠可以理解为对样式的覆盖,优先性为: 网站开发者的样式表 用户样式(通过设置浏览器的显示选项) 浏览器默认的样式 ...

  6. css样式表中的样式覆盖顺序

    刚才写zenktodo的时候,通过动态添加class的方式修改一个div的样式,总是不起作用. #navigator { height: 100%; width: 200; position: abs ...

  7. css样式表中的样式覆盖顺序(转)

    有时候在写CSS的过程中,某些限制总是不起作用,这就涉及了CSS样式覆盖的问题,如下 Css代码   #navigator { height: 100%; width: 200; position:  ...

  8. css样式表中四种属性选择器

    学习此连接的总结http://developer.51cto.com/art/201009/226158.htmcss样式表中四种属性选择器1> 简易属性 tag[class]{ font-we ...

  9. 一个DOM元素同时拥有多个类名时的样式产生冲突时 属性取决于css样式表中后读取到的属性

    如果一个DOM元素包含多个类名,其中的两个类名的属性产生冲突,并不是根据htnl中类名的顺序来决定DOM元素的属性, 而是根据css样式中的顺序来决定DOM元素的属性,它取决于css样式表中后读取到的 ...

随机推荐

  1. orcale装完sqldevelop启动不了

    一直在搞考试,昨天考java企业级开发要交项目搞得我装系统后又装了个orcale,每次重新配百度太麻烦,还好记得点,记录下碰到的错误 64位的系统下的orcale11 64位里面的sqldevelop ...

  2. openStack 王者归来之 trivial matters

    <一,openStack img 制作> tips:制作大部分cloud platforms img准备工作. <1,> http://www.pubyun.com/blog/ ...

  3. 再学习sqlhelper

    在机房收费重构系统的时候,第一次学习sqlhelper.当时感觉比较简单,没有写博客总结,现在又经过了图书馆的学习,感觉还是有必要写一写的. SqlHelper是一个基于.NETFramework的数 ...

  4. HTML DOM 创建与修改

    修改 HTML 元素 修改 HTML DOM 意味着许多不同的方面: 改变 HTML 内容 改变 CSS 样式 改变 HTML 属性 创建新的 HTML 元素 删除已有的 HTML 元素 改变事件(处 ...

  5. compass安装教程

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  6. If the server requires more time, try increasing the timeout in the server editor

    双击服务器,在overview下的Timeouts中的Start选项,改成10000或者较大就可以了.防止服务器自启动频繁.

  7. C#获取上个月的第一天零点和最后一天23点59分59秒

    //上个月第一天0点 DateTime date2 = DateTime.Now.AddMonths(-1).Date.AddDays(1 - DateTime.Now.Day);         R ...

  8. uva 11134 - Fabled Rooks(问题转换+优先队列)

    题目链接:uva 11134 - Fabled Rooks 题目大意:给出n,表示要在n*n的矩阵上放置n个车,并且保证第i辆车在第i个区间上,每个区间给出左上角和右小角的坐标.另要求任意两个车之间不 ...

  9. 手动添加删除windows服务

    1.使用sc命令创建服务 命令格式如: sc create [service name] [binPath= ] <option1> <option2>... 比如: sc c ...

  10. linxu添加定时任务

    1. 在需要定时执行的php文件的第一行加 也就是文件开头加  #! /bin/local/php -q 其中 /bin/local/php 是自己的php.exe 所在的位置 2. 上传要定时执行的 ...