开发者

Can I access externally defined styles using the JS DOM?

开发者 https://www.devze.com 2023-01-25 21:33 出处:网络
If I use jQuery, I can get at externally defined styles like: $(\"#element\").css(\"background-image\")

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

精彩评论

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