获取css style值
2016-01-01 12:44
603 查看
var obj=document.getElementById("btn");
var currentStyle=null;
if(obj.currentStyle){
currentStyle=obj.currentStyle;
}else{
currentStyle=window.getComputedStyle(obj,null);
}
alert(currentStyle.width);
var addCssRule = function() {
// 创建一个 style, 返回其 stylesheet 对象
// 注意:IE6/7/8中使用 style.stylesheet,其它浏览器 style.sheet
function createStyleSheet() {
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
head.appendChild(style);
console.dir(style)
return style.sheet ||style.styleSheet;
}
// 创建 stylesheet 对象
var sheet = createStyleSheet();
// 返回接口函数
return function(selector, rules, index) {
index = index || 0;
if (sheet.insertRule) {
sheet.insertRule(selector + "{" + rules + "}", index);
} else if (sheet.addRule) {
sheet.addRule(selector, rules, index);
}
}
}();
addCssRule('p', 'color:red;border:1px solid gray;');
addCssRule('div', 'color:yellow;border:1px solid gray;');
var currentStyle=null;
if(obj.currentStyle){
currentStyle=obj.currentStyle;
}else{
currentStyle=window.getComputedStyle(obj,null);
}
alert(currentStyle.width);
var addCssRule = function() {
// 创建一个 style, 返回其 stylesheet 对象
// 注意:IE6/7/8中使用 style.stylesheet,其它浏览器 style.sheet
function createStyleSheet() {
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
head.appendChild(style);
console.dir(style)
return style.sheet ||style.styleSheet;
}
// 创建 stylesheet 对象
var sheet = createStyleSheet();
// 返回接口函数
return function(selector, rules, index) {
index = index || 0;
if (sheet.insertRule) {
sheet.insertRule(selector + "{" + rules + "}", index);
} else if (sheet.addRule) {
sheet.addRule(selector, rules, index);
}
}
}();
addCssRule('p', 'color:red;border:1px solid gray;');
addCssRule('div', 'color:yellow;border:1px solid gray;');
相关文章推荐
- 保证CSS与JS最新
- CSS3 笔记二(Gradients)
- CSS 垂直居中。
- CSS 巧用 :before和:after
- Nancy Scripts,CSS文件夹配置
- TextView显示html样式的文字
- css中pt、px、em、ex、in等这类长度单位详细说明
- 专为控制打印设计的CSS样式
- input的值为浅淡样式(点击值消失)
- data-role="navbar" 导航框样式被挤下去
- PyCharm中HTML页面CSS class名称自动完成功能失效的问题
- 16.定位模板,布局和样式
- CSS属性简写
- WPF自定义Button样式(按钮长度随Content长度自适应)
- css bottom top left right
- CSS3:text-overflow实现文字截取,超出部分显示省略号
- CSS透明属性详解
- EditText 隐藏密码 字体样式被改变
- Hack、自适应、针对浏览器写样式随手笔记
- CSS块级元素和行内元素