在此代码中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>
答案 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);