单击菜单栏时,JScript隐藏徽标

时间:2019-02-21 07:20:41

标签: javascript

我想隐藏以前的徽标和菜单栏,但是当我单击菜单栏时。但是结果出乎意料。

enter image description here

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";
    }
}

1 个答案:

答案 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";
    }
}