开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜