我想隐藏以前的徽标和菜单栏,但是当我单击菜单栏时。但是结果出乎意料。
HTML:
<div class="header">
<img src="common/img/logo.png" class="logo">
<input type="checkbox" id="chk">
<label for="chk" class="show-menu-btn" onclick="myChk()">
<i class="fas fa-bars"></i>
</label>
<label for="chk" class="hide-menu-btn">
<i class="fas fa-times"></i>
</label>
脚本:
function myChk(){
var image = document.getElementById("show-menu-btn");
if(image.src.match("show-menu-btn")){
image.src = "none";
}else{
image.src = "common/img/logo.png";
}
}
答案 0 :(得分:1)
代替更改src,您可以执行image.style.diplay ='none'进行隐藏,并执行image.style.diplay ='inline-block'进行显示。
HTML
<div class="header">
<img src="common/img/logo.png" class="logo" id="logo">
<input type="checkbox" id="chk">
<label for="chk" class="show-menu-btn" onclick="myChk()">
<i class="fas fa-bars"></i>
</label>
<label for="chk" class="hide-menu-btn">
<i class="fas fa-times"></i>
</label>
</div>
JS:
function myChk(){
var image = document.getElementById("logo");
if(image.style.display === 'none'){
image.style.display = "inline-block";
}else{
image.style.display = "none";
}
}