代码之家  ›  专栏  ›  技术社区  ›  Kyle Cureau

使用jquery获取*所有*css属性

  •  21
  • Kyle Cureau  · 技术社区  · 14 年前

    下面是如何使用jquery获得一个css属性:

    $('someObject').css('attribute')

    你怎么把它们都弄到手?(不指定,最好采用以下格式,以便以后可以使用jquery重新应用):

        cssObj = {
            'overflow':'hidden',
            'height':'100%',
            'position':'absolute',
        }
    

    谢谢!!

    编辑

    我尝试获取的方法在样式表中声明(它们不是内联的)。很抱歉没有说明。

    4 回复  |  直到 7 年前
        1
  •  9
  •   Community CDub    8 年前

    像这样的东西怎么样:

    jQuery CSS plugin that returns computed style of element to pseudo clone that element?

    它很难看,但看起来对海报很有用…

    这也可能引起人们的兴趣: https://developer.mozilla.org/en/DOM:window.getComputedStyle

        2
  •  14
  •   jacktheripper    13 年前

    看到这个 live example 使用jquery属性选择器

    $(document).ready(function() {
        alert($("#stylediv").attr('style'));
    });​
    
        3
  •  5
  •   pyrospade    10 年前

    不知道这个浏览器有多跨浏览器,但它在Chrome中工作。-

    https://gist.github.com/carymrobbins/223de0b98504ac9bd654

    var getCss = function(el) {
        var style = window.getComputedStyle(el);
        return Object.keys(style).reduce(function(acc, k) {
            var name = style[k],
                value = style.getPropertyValue(name);
            if (value !== null) {
                acc[name] = value;
            }
            return acc;
        }, {});
    };
    
        4
  •  1
  •   Surender Lohia    7 年前

    window.getComputedStyle(元素);

    // For example
    var element = document.getElementById('header');
    window.getComputedStyle(element);