自定义类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样式表中后读取到的 ...
随机推荐
- kbengine环境搭建(2)
做好准备工作后,可以开始搭建我们的kbengine服务端,运行成功kbengine服务端,共有9个服务会相应的被打开,并会全部提示[info]found all components! 准备工作 1. ...
- FPGA STA(静态时序分析)
1 FPGA设计过程中所遇到的路径有输入到触发器,触发器到触发器,触发器到输出,例如以下图所看到的: 这些路径与输入延时输出延时,建立和保持时序有关. 2. 应用背景 静态时序分析简称STA,它是一种 ...
- 我的Hook学习笔记
关于Hook 一.基本概念: 钩子(Hook),是Windows消息处理机制的一个平台,应用程序能够在上面设置子程以监视指定窗体的某种消息,并且所监视的窗体能够是其它进程所创建的.当消息到达后,在目标 ...
- ios7高级
1.实现控制器和视图. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 ...
- POJ 3974 Palindrome(最长回文子串)
题目链接:http://poj.org/problem?id=3974 题意:求一给定字符串最长回文子串的长度 思路:直接套模板manacher算法 code: #include <cstdio ...
- 原生js实现的轮播图,易用+可多用
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度
highcharts图表组件入门教程:如何监听柱状图柱子点击事件动态更新当前数据点数值和所对应X轴刻度 作者:highcharts | 时间:2014-6-11 14:07:05 | [小 大] | ...
- cocos2dx定时器
cocos2dx三种定时器的使用以及停止schedule,scheduleUpdate,scheduleOnce 首先,什么是定时器呢?或许你有时候会想让某个函数不断的去执行,或许只是执行一次,获取你 ...
- 帝国cms7.2灵动标签万能教程
学完本文,就完全能掌握帝国模板开发制作啦!这里只介绍sql语句调用方法(方便,快捷!) 灵动标签语法: [e:loop={,24,0}] 模板内容 [/e:loop] 详细解释:黄色部分:条件语句,即 ...
- RatProxy
http://book.51cto.com/art/201212/374023.htm http://www.oschina.net/p/ratproxy/similar_projects