简体   繁体   English

为什么它不起作用,style.display

[英]Why it doesn't work, style.display

I have a short script. 我有一个简短的剧本。 I want to check the visibility oder the Display of a div. 我想检查div显示的可见性。 But it doesn't work. 但这是行不通的。 And i have no idea why, i found some other scripts with the same Code. 而且我不知道为什么,我发现了其他一些具有相同代码的脚本。

I didn't get the message JA 我没有收到消息JA

<html>
<head>
<script>
    function test()
    {
        alert('J');
        if (document.getElementById('t').style.visibility == "visible")
        {
            alert('JA');
        }
        if (document.getElementById('t').style.display == "block")
        {
            alert('JA');
        }
    }
</script>
</head>
<body>
<div id = 't'>
    test
</div>
<input type = 'button' onClick = 'test();'>

</body>
</html>

The style property only represents the CSS directives specified in the element's style attribute therefore, both visibility and display will be empty. style属性仅表示在元素的style属性中指定的CSS指令,因此visibilitydisplay均为空。

What you want is computed style 您想要的是计算样式

var t = document.getElementById('t'),
    style = window.getComputedStyle(t);

console.log('visibility', style.visibility);
console.log('display', style.display);

JSFiddle Demo JSFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM