您的位置:首页 > Web前端 > CSS

获取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;');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: