就是想点击按钮时如果是none的话就让它变成block 如果是block的话就让它变成none 为什么只能变一次呢?想再变消失却不行呢
<html>
<head>
<meta charset="utf-8">
<style>
#p1{width:400px;height: 400px;background-color: red;display: none;}
</style>
<script>
window.onload=function (){
var op=document.getElementById('p1');
var oBtn=document.getElementById('btn1');
oBtn.onclick=function(){
if(op.style.display=="none"){
op.style.display="block";
}else{
op.style.display="none";
}
}
}
</script>
</head>
<body>
<input id="btn1" type="button" value="按钮">
<p id="p1">
</p>
</body>
</html>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
两个问题
1.if(op.style.display="none") 改为==或者===;
2.style只能获得内联样式。
因为你写的判断是 if(op.style.display="none"){ 一个=是赋值,==才是判断。
上面说了,style只能拿内联样式
你要拿css内的样式需要这样拿styles = getComputedStyle(op);
styles.display
但是这样不能修改或者是不推荐修改,你要修改的话,还是用op.style.display = 'xxx';这样修改