getComputedStyle(或)$ .css(map)< - 获取每个样式声明 [英] getComputedStyle (or) $.css(map) <-- to get every style declaration

查看:108
本文介绍了getComputedStyle(或)$ .css(map)< - 获取每个样式声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了循环遍历每个样式属性声明的数组之外,有没有办法获得dom元素所有样式的键/值输出?

Aside from looping through an array that has each style property declared, is there any way to get a key/value output of all styling of a dom element?

我的备用程序通过列出的键迭代:
http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSview-getComputedStyle

My fallback is iterating through the keys listed on: http://www.w3.org/TR/DOM-Level-2-Style/css.html#CSS-CSSview-getComputedStyle

推荐答案


有没有办法得到dom元素的所有样式的键/值输出?

is there any way to get a key/value output of all styling of a dom element?

是的,但不要期望对值(单位等)的确切处理是同一个跨浏览器。

Yes, but don't expect the exact handling of values (units etc.) to be the same cross-browser.

var styles= [];

// The DOM Level 2 CSS way
//
if ('getComputedStyle' in window) {
    var cs= getComputedStyle(element, '');
    if (cs.length!==0)
        for (var i= 0; i<cs.length; i++)
            styles.push([cs.item(i), cs.getPropertyValue(cs.item(i))]);

    // Opera workaround. Opera doesn't support `item`/`length`
    // on CSSStyleDeclaration.
    //
    else
        for (var k in cs)
            if (cs.hasOwnProperty(k))
                styles.push([k, cs[k]]);

// The IE way
//
} else if ('currentStyle' in element) {
    var cs= element.currentStyle;
    for (var k in cs)
        styles.push([k, cs[k]]);
}

这篇关于getComputedStyle(或)$ .css(map)&lt; - 获取每个样式声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆