Can I access externally defined styles using the JS DOM?
If I use jQuery, I can get at externally defined styles like:
$("#element").css("background-image")
If I try to do this without jQuery, like
document.getElementById("element").sty开发者_JAVA技巧le.backgroundImage
I get an empty string back. Is there a way to get this information without jQuery?
Yes. The painful way.
function getStyle(el,styleProp)
{
var x = document.getElementById(el);
if (x.currentStyle)
var y = x.currentStyle[styleProp];
else if (window.getComputedStyle)
var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
return y;
}
Stolen from quirksmode. This is probably a more succinct version of the jQuery static method.
精彩评论