0

我想获取元素内联、外部、内部的所有 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

4

0 回答 0