为什么div与id =子显示值是""而不是没有

时间:2016-05-26 16:51:45

标签: javascript html css

在此代码中css显示属性div,其中id =" sub"已更改为none,但在运行js代码后,它显示其css显示值为空("")

<head>
<style>
    #title {
        height: 60px;
        background-color: green;
    }  
    #sub {
        height: 100px;
        background-color: orange;
        display: none ;
    }
<!-- display for sub set to none-->
</style>
</head>
<body>
    <div id="title"  onmousedown= "showhide('sub')"></div>
    <div id= "sub"  ></div>

<script>
    function showhide(box)  {
        var border = document.getElementById(box);
        alert('div '+ box+' got');


    if(border.style.display ==="" ){
        alert( border.style.display);
        alert ('if is true so display changes to block');
        border.style.display = "block";

    }
    else{
        alert( border.style.display);
        alert ('else is true so display changes to " "');
        border.style.display = "";
   }         
</script>
</body>

1 个答案:

答案 0 :(得分:0)

您需要使用getComputedStyle

var elem = document.getElementById(box);
if (elem.currentStyle) {
    var displayStyle = elem.currentStyle.display;
} else if (window.getComputedStyle) {
    var displayStyle = window.getComputedStyle(elem, null).getPropertyValue("display");
}
alert(displayStyle);