简体   繁体   English

jQuery if / else if语句优先级

[英]jQuery if / else if statement priorities

I am trying to understand why my code does not work the way I want it to. 我试图理解为什么我的代码无法按我想要的方式工作。 In the comments I am expalaining what each part means. 在评论中,我解释了每个部分的含义。

Here is my code: 这是我的代码:

$(document).keyup(function(event){
    // if hit > arrow key
    if(event.keyCode == 39){

      // if nav's level 1 dropdown list is not displayed, use > arrow in level 0
      if ($('nav li .sub-menu').css('display', 'none')){
        $('nav li.active').next().addClass('active').siblings().removeClass('active');


      // else if level 1 dropdown list is displayed, use > arrow in level 1
      }else if ($('nav li.active .sub-menu').css('display', 'block') && $('nav .sub-menu li').hasClass('has-child')){ 


         // open level 2 dropdown list
         $('nav li.active .sub-menu li.has-child').find('.sub-sub-menu').stop().hide(); 
      }
    }        
});

The problem is, it does not go into else if statement, no matter if dropdown list is displayed or not, therefore > arrow keeps working in level 0 (nav). 问题是,无论是否显示下拉列表,它都不会进入else if语句,因此>箭头始终在0级(nav)中起作用。 If I use if statement instead else if, > arrow works both in level 0 and level 1, and its not what I am looking for. 如果我使用if语句代替else if,>箭头可同时在0级和1级中使用,而这并不是我想要的。

If anyone knows whats wrong with my code, I would appreciate to hear your answer. 如果有人知道我的代码有什么问题,不胜感激。 Thank you. 谢谢。

$('nav li .sub-menu').css('display', 'none') sets the display to none , it does not check its value. $('nav li .sub-menu').css('display', 'none')将显示设置为none ,不检查其值。 That is why it is not working. 这就是为什么它不起作用。

What you want to do is: 您想要做的是:

if($('nav li .sub-menu').css('display') === 'none)

The same goes for the else statement. else语句也是else You should have something like this instead: 您应该改为以下内容:

$(document).keyup(function(event){
    // if hit > arrow key
    if(event.keyCode == 39){
      var nav = $('nav li .sub-menu');
      var navDisplay = nav.css('display');

      // if nav's level 1 dropdown list is not displayed, use > arrow in level 0
      if (navDisplay === 'none'){
        $('nav li.active').next().addClass('active').siblings().removeClass('active');


      // else if level 1 dropdown list is displayed, use > arrow in level 1
      }else if (navDisplay === 'block' && nav.hasClass('has-child')){ 


         // open level 2 dropdown list
         $('nav li.active .sub-menu li.has-child').find('.sub-sub-menu').stop().hide(); 
      }
    }        
});

Try to get into the habit of storing the results of your jQuery calls if you know you are going to reuse them, these DOM calls can get pretty expensive. 如果您知道要重用它们,请尝试养成存储jQuery调用结果的习惯,这些DOM调用可能会变得非常昂贵。

You have several options here. 您在这里有几种选择。

You can do: 你可以做:

if($('nav li .sub-menu').css('display') === 'none'))

OR 要么

if($('nav li .sub-menu:visible'))

OR 要么

if($('nav li .sub-menu').is(":visible")))

The last one is the prettiest and pretty standard. 最后一个是最漂亮,最漂亮的标准。

you are setting the display not checking the state. 您正在设置显示而不检查状态。 try this to check if your element is hidden or not 试试这个检查您的元素是否被隐藏

$('nav li.active .sub-menu').is(":visible") 

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

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