自定义类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--) {
//~~~~rules.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跨浏览器操作样式表中的规则

时间: 2024-10-25 05:46:17

自定义类StyleSheet跨浏览器操作样式表中的规则的相关文章

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

转自:http://spartan1.iteye.com/blog/1526735 有时候在写CSS的过程中,某些限制总是不起作用,这就涉及了CSS样式覆盖的问题,如下 Css代码 #navigator { height: 100%; width: 200; position: absolute; left: 0; border: solid 2 #EEE; } .current_block { border: solid 2 #AE0; } 查找一些教材中(w3schools等),只说css的

Andriod React Native 样式表中可用样式属性

写了这么多篇Android React Native的博文,基本上把复杂的东西都搞定了,接下来来看看一些轻松的东西,和布局有关,就是css样式,那么一个View可以设置哪些css样式呢,是和web中的css样式完全一样呢,还是有所不同呢?其实你只要在样式表中书写一个不存在的样式,就会报一大堆错,提示你该样式不存在,然后提供所有可用的样式给你,如图 下面的样式就是样式表中所有可用的属性. "alignItems", "alignSelf", "backfac

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

css样式表中的样式覆盖顺序 - - ITeye技术网站http://spartan1.iteye.com/blog/1526735 重点归纳: 查找一些教材中(w3schools等),只说css的顺序是“元素上的style”>“文件头上的style元素”>“外部样式文件”,但对于样式文件中的多个相同样式的优先级怎样排列,没有详细说明.经过测试和继续搜索,得知优先级如下排列: 或者以下也可以称之为一些优先原则(by liwei 20140803) 1.样式表的元素选择器选择越精确,则其中的样式

样式表中的样式及其布局常用标签

样式表中的样式及其布局1.大小:width宽度,height高度.2.背景:backgrpund-color,backgrpund-image,backgrpund-repeat,backgrpund-position,backgrpund-attachment,backgrpund-size.3.字体:font-family,font-size,font-style,font-weight,text-decoration:(underline,overline,lin-through,none

在XSLT样式表中插入VBScript脚本进行数学计算

继上次我写了篇文章<在XSLT样式表中使用莱布尼兹级数计算π的近似值>后,我发现在XSLT中使用模板的递归调用,有下面两个缺点:1)易读性差,有失灵活:2)效率低下.因此,我又想了一个新的策略实现在XSLT中的一些复杂的计算工作,这就是在XSLT中嵌入脚本,本文以VBScript为例描述了这一过程 现有XML文档pi.xml如下: <?xml version="1.0" encoding="utf-8"?> <?xml-styleshe

css样式表中设置table的第一列的宽度是固定值

table{table-layout:fixed;}table tr td:first-child,table tr td:first-child{width:120px;} 首行第一个td定宽同列的宽度都会和他一样. *注意 第一行 第一个用的是 td 还是 th css样式表中设置table的第一列的宽度是固定值,布布扣,bubuko.com

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

css样式表中四种属性选择器1> 简易属性 tag[class]{ font-weight:bold } It will affect all tag with any class. e.g. <h2 class="old" > or <h2 class="new"> 2>精确属性值 a[href="http://www.cnblogs.cn"][title="textTitle"]{fon

CSS 样式表中padding 的用法

CSS中padding 描述的是内边距的意思,margin是外边距.QSS中同样的道理. 1. padding表示内边距的意思,margin---外边距.2. CSS的 padding 属性定义元素边框与元素内容之间的空白区域.3. padding 接受长度值或百分比值,但不允许使用负值.4. 示例:如果希望所有内边距都是10 像素,只需要设置:padding:10px; 就可以了.5. 也可以分别设置4个边的内边距,只需通过它们的单独属性,分别设置上.右.下.左内边距:* padding-to

DOM操作样式表

1.操作Style样式属性 1.1通过style对象的属性 document.getElementById("bbb").style.backgroundColor="red"; //由style的属性值访问或者获得的是内联样式 1.2通过style对象的方法//由于IE不支持,所以用第一种方法较好 getPropertyValue(propertyName);//用CSS属性中的写法:background-color getPropertyPriority();