0
<div class="a"></div>

.a{
padding:10px;
background:red;
}

it's easy to get above element's background value, just use $('.a').css('background');

but now I have this div :

<div style="padding:0;background:red"></div>

How can I get the background property?

James Lemon
  • 426
  • 5
  • 17

1 Answers1

0

You should use getComputedStyle:

getComputedStyle($('div')[0]).background; 

From the MDN article:

The object returned from getComputedStyle is read-only and can be used to inspect the element's style (including those set by a element or an external stylesheet). The elt.style object should be used to set styles on a specific element.

lxe
  • 7,051
  • 2
  • 20
  • 32
  • 1
    @JamesLemon That gets the DOM element out of the jQuery object. Because `getComputedStyle` is a DOM function, not part of jQuery. – Barmar Apr 03 '15 at 02:23