代码之家  ›  专栏  ›  技术社区  ›  Richard H

获取DOM元素(la Firebug)的所有css样式

  •  6
  • Richard H  · 技术社区  · 15 年前

    还是有更优雅的方式?萤火虫是怎么做到的?

    谢谢

    3 回复  |  直到 15 年前
        1
  •  11
  •   NoBugs    11 年前

    你应该可以得到它 getComputedStyle :

    var css = window.getComputedStyle(element);
    for (var i=0; i<css.length; i++) {
        console.log(css[i] +'='+css.getPropertyValue(""+css[i]))
    }
    
        2
  •  -1
  •   JasonWyatt    15 年前

    var myElement = document.getElementById('someId');
    var myElementStyle = myElement.style;
    
    for(var i in myElementStyle){
        // if it's not a number-index, print it out.
        if(/^[\d]+/.exec(i) == null){
            console.log("Style %s = %s", i, myElementStyle[i]);
            /*
             * Do other stuff here...
             */
        }
    }
        3
  •  -2
  •   Community CDub    8 年前

    对于DOM元素,如何获取所有 在css中指定的样式 特殊元素?是这样的吗 遍历所有css样式名?

    还是有更优雅的方式?

    我不知道是否更优雅(优雅在主观尺度上是相当高的),但如果使用jQuery这样的库,它肯定会更短、更甜美,这里有一个解决方案,有人编写代码来回答另一个问题:

    How Can I Get List Of All Element CSS Attributes with jQuery?

    萤火虫是怎么做到的?

    我不知道。