如何检索DOM元素的显示属性? [英] How to retrieve the display property of a DOM element?

查看:70
本文介绍了如何检索DOM元素的显示属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
    <style type="text/css">
        a {
            display: none;
        }
    </style>
    <body>
        <p id="p"> a paragraph </p>
        <a href="http://www.google.com" id="a">google</a>
    </body>
    <script type="text/javascript">
        var a = (document.getElementById('a')).style;
        alert(a.display);
        var p = (document.getElementById('p')).style;
        alert(p.display);
        p.display = 'none';
        alert(p.display);
    </script>
</html>

第一个和第二个警告除了一个空字符串,我认为应该是
但是在强烈的显示设置后,第三个 alert finally alert / code>。

The first and the second alert display nothing other than a empty string, which I thought should be none and block. However after the intensionally display setting, the third alert finally alert none.

但是为什么?如何正确检索 display 属性?

But Why? How could I retrieve the display property correctly?

谢谢。

推荐答案

.style。* 属性直接映射到 style 属性,而不是应用的样式。为此,您需要 getComputedStyle

The .style.* properties map directly onto the style attribute, not to the applied style. For that you want getComputedStyle.

我会认真考虑toggling .className ,并将演示文稿与逻辑分开。

I'd give serious consideration to toggling .className and separating the presentation from the logic entirely.

这篇关于如何检索DOM元素的显示属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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