使用 Javascript 将 CSS 标记输出到字符串

发布于 2024-11-19 01:27:04 字数 823 浏览 6 评论 0原文

我想知道是否有人有尝试使用 javascript 输出给定元素的 css 标记的经验。

注意:仅使用Webkit。

例如:

HTML:

<div id="css"></div>

样式:

#css {
    background: #F1F3F5;
    border: 1px solid #B4BFC9;

    -moz-box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
    -webkit-box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
    box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
}

漂亮的 jQuery 插件? :)

alert($('#css').getCSS());

警报

#css {
    background: #F1F3F5;
    border: 1px solid #B4BFC9;

    -moz-box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
    -webkit-box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
    box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
}

I'm wondering if anyone has had experience trying to output the css markup of a given element using javascript.

Note: Using Webkit only.

For example:

HTML:

<div id="css"></div>

Styling:

#css {
    background: #F1F3F5;
    border: 1px solid #B4BFC9;

    -moz-box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
    -webkit-box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
    box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
}

nifty jQuery plugin? :)

alert($('#css').getCSS());

Alert

#css {
    background: #F1F3F5;
    border: 1px solid #B4BFC9;

    -moz-box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
    -webkit-box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
    box-shadow: 0px 1px rgba(0, 0, 0, 0.3);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年慕年 2024-11-26 01:27:04
function css(a){
    var sheets = document.styleSheets, o = {};
    for(var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for(var r in rules) {
            if(a.is(rules[r].selectorText)) {
                o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
            }
        }
    }
    return o;
}

function css2json(css){
        var s = {};
        if(!css) return s;
        if(css instanceof CSSStyleDeclaration) {
            for(var i in css) {
                if((css[i]).toLowerCase) {
                    s[(css[i]).toLowerCase()] = (css[css[i]]);
                }
            }
        } else if(typeof css == "string") {
            css = css.split("; ");          
            for (var i in css) {
                var l = css[i].split(": ");
                s[l[0].toLowerCase()] = (l[1]);
            };
        }
        return s;
    }

用法:

var style = css($("#elementToGetAllCSS"));
$("#elementToPutStyleInto").css(style);
function css(a){
    var sheets = document.styleSheets, o = {};
    for(var i in sheets) {
        var rules = sheets[i].rules || sheets[i].cssRules;
        for(var r in rules) {
            if(a.is(rules[r].selectorText)) {
                o = $.extend(o, css2json(rules[r].style), css2json(a.attr('style')));
            }
        }
    }
    return o;
}

function css2json(css){
        var s = {};
        if(!css) return s;
        if(css instanceof CSSStyleDeclaration) {
            for(var i in css) {
                if((css[i]).toLowerCase) {
                    s[(css[i]).toLowerCase()] = (css[css[i]]);
                }
            }
        } else if(typeof css == "string") {
            css = css.split("; ");          
            for (var i in css) {
                var l = css[i].split(": ");
                s[l[0].toLowerCase()] = (l[1]);
            };
        }
        return s;
    }

Usage:

var style = css($("#elementToGetAllCSS"));
$("#elementToPutStyleInto").css(style);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文