简体   繁体   English

Javascript if / else没有按预期工作

[英]Javascript if/else not working as expected

I am trying to toggle a hidden menu with a link. 我试图用链接切换隐藏的菜单。 But when i click on the link, it reopens the hidden menu instead of closing it . 但是当我点击链接时,它会重新打开隐藏的菜单而不是关闭它

Here is how i expect it to run: 以下是我希望它运行的方式:

  • When i click labelLink 当我点击labelLink
    if hiddenBox 's display = 'none' , then change it to display = 'block' 如果hiddenBoxdisplay = 'none' ,则将其更改为display = 'block'
    if hiddenBox 's display = 'block' , then delete its focus by blur() and set it display='none' 如果hiddenBoxdisplay = 'block' ,则通过blur()删除其焦点并将其设置为display='none'

  • When i click outside of the hiddenBox when it has the focus, set hiddenBox 's display='none' 当我在hiddenBox的焦点外面点击时,设置hiddenBox的display='none'
    ( This part is working well. ) 这部分运作良好。

JsFiddle 的jsfiddle

<ul>
    <li> 
        <a id="labelLink"  href="#" 
            onclick="
                if(document.getElementById('hiddenBox').style.display === 'block'){
                    document.getElementById('labelLink').focus();
                    document.getElementById('hiddenBox').style.display ='none';
                }
                else{
                    document.getElementById('hiddenBox').style.display = 'block';
                    document.getElementById('hiddenBox').focus();
                }
                return false;"
        >
        Click Me
        </a>
        <div id="hiddenBox" tabindex="-1"  
            onblur="document.getElementById('hiddenBox').style.display ='none';"
        >
        I was hidden
        </div>
    </li>
</ul>

Do it this way instead. 这样做是这样的。

 var labelLink = document.getElementById('labelLink'); var hiddenBox = document.getElementById('hiddenBox'); labelLink.addEventListener('click', function() { hiddenBox.style.display = hiddenBox.style.display == 'block' ? 'none': 'block'; }); labelLink.addEventListener('blur', function() { hiddenBox.style.display = 'none'; }) 
 #hiddenBox { display: none } 
 <ul> <li><a id="labelLink" href="#">Click Me</a> <div id="hiddenBox" tabindex="-1">I was hidden</div> </li> </ul> 

As already pointed out, the two event listeners are interfering with each other. 正如已经指出的那样,两个事件监听器互相干扰。 One way of fixing this is to remove the listener on labelLink when the hidden box is shown, and restore the listener with a slight pause after the hidden box is hidden again. 修复此问题的一种方法是在显示隐藏框时删除labelLink上的侦听器,并在隐藏框再次隐藏后稍微暂停恢复侦听器。 JSFiddle 的jsfiddle

 var labelLink = document.getElementById('labelLink'), hiddenBox = document.getElementById('hiddenBox'); labelLink.addEventListener('click', showBox); hiddenBox.addEventListener('blur', hideBox); function showBox(){ hiddenBox.style.display = 'block'; hiddenBox.focus(); labelLink.removeEventListener('click', showBox); } function hideBox() { hiddenBox.style.display = 'none'; labelLink.focus(); window.setTimeout(function() { labelLink.addEventListener('click', showBox); }, 500); } 
 <a id="labelLink" href="#" >Click Me</a> <div id="hiddenBox" tabindex="-1" style="display:none" >I was hidden</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM