使用javascript从外部css文件中获取元素的样式 [英] Use javascript to get the style of an element from an external css file

查看:97
本文介绍了使用javascript从外部css文件中获取元素的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的html:

 < html> 
< head>
< link rel =stylesheettype =text / cssmedia =allhref =style.css>
< / head>

< body>
< div id =test>测试< / div>

< script>
alert(document.getElementById('test')。style.display);
< / script>
< / body>
< / html>

style.css:

  div {
display:none;
}

我希望js会返回none,但它会返回一个空字符串代替。有什么办法可以解决这个问题吗?解析方案

这可以用于符合标准的浏览器(而不是IE - currentStyle / runtimeStyle)。




 <身体GT; 
< div id =test>测试< / div>

< script type =text / javascript>
window.onload = function(){
alert(window.getComputedStyle(document.getElementById('test'),null).getPropertyValue('display'));
}
< / script>
< / body>


I have a html like this:

<html>
    <head>
        <link rel="stylesheet" type="text/css" media="all" href="style.css">
    </head>

    <body>
        <div id="test">Testing</div>

        <script>
            alert(document.getElementById('test').style.display);
        </script>
    </body>
</html>

The style.css:

div {
    display:none;
}

I expect the js would return "none", but it return an empty string instead. Is there any way to solve this problem?

解决方案

This would work for standards compliant browsers (not IE - currentStyle/runtimeStyle).

<body>
    <div id="test">Testing</div>

    <script type="text/javascript">
        window.onload = function() {
            alert(window.getComputedStyle(document.getElementById('test'),null).getPropertyValue('display'));
        }
    </script>
</body>

这篇关于使用javascript从外部css文件中获取元素的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆