jQuery - How to get all styles/css (defined within internal/external document) with HTML of an element

前端 未结 9 1992
不知归路
不知归路 2020-12-01 08:22

I know that $(\"#divId\").html() will give me innerHtml. I also need its styles (which might be defined by the means of classes) either in-line style

9条回答
  •  爱一瞬间的悲伤
    2020-12-01 08:38

    You can get hold of a style object representing the computed style for an element using window.getComputedStyle() in most browsers and the element's currentStyle property in IE. There are several browser differences, however, with values returned for shortcut properties (such as background), color RGB values, lengths and even font-weight (see this useful test page). Test carefully.

    function computedStyle(el) {
        return el.currentStyle || window.getComputedStyle(el, null);
    }
    
    alert(computedStyle(document.body).color);
    

提交回复
热议问题