我想获取内联,外部,内部元素的所有css属性。
我正在使用以下代码:
对于内部和外部css:
function getElementChildrenAndStyles(selector) {
selector = selector.split(",").map(function(subselector){
return subselector + "," + subselector + " *";
}).join(",");
elts = $(selector);
var rulesUsed = [];
// main part: walking through all declared style rules
// and checking, whether it is applied to some element
sheets = document.styleSheets;
for(var c = 0; c < sheets.length; c++) {
var rules = sheets[c].rules || sheets[c].cssRules;
for(var r = 0; r < rules.length; r++) {
var selectorText = rules[r].selectorText;
var matchedElts = $(selectorText);
for (var i = 0; i < elts.length; i++) {
if (matchedElts.index(elts[i]) != -1) {
rulesUsed.push(rules[r]); break;
}
}
}
}
var style = rulesUsed.map(function(cssRule){
var cssText = cssRule.cssText;
// some beautifying of css
return cssText.replace(/(\{|;)\s+/g, "\$1\n ").replace(/\A\s+}/, "}");
// set indent for css here ^
}).join("\n");
return style;
}
对于内联css:
document.getElementById('elemid').style.cssText;
这在所有浏览器中都运行良好。
但问题是,当我警告css时,它会显示边界的详细属性,如:
border-top-color:#ccc
border-left-color:#ccc
border-bottom-color:#ccc
border-right-color:#ccc
border-top-width:1px
border-left-width:1px
border-bottom-width:1px
border-right-width:1px
border-top-style:solid
border-left-style:solid
border-bottom-style:solid
border-right-style:solid
这种情况只发生在Opera n Safari ......
我只想要一条线css用于边框 例如边框:1px solid #ccc