简体   繁体   English

了解Javascript三元语句代码段

[英]Understanding Javascript ternary statement code snippet

I came across below code snippet and trying to understand it. 我遇到了下面的代码片段,并试图理解它。 I understand that we are using ternary operator and adding active class on menuclick but a detailed explanation will be appreciated. 我了解我们正在使用三元运算符,并在menuclick上添加了活动类,但将不胜枚举。

navClick: function (o) { 
var _this = this //what does this refer to

//what does this line of code do especially the equal sign
!_this.menuclicked ?(($(".last-menuitem").attr("id")==$("#menu li.active").find("a:last-child").not(".ignore-ele").attr("class") 
|| $(".last-menuitem").find(".view-holder").attr("id")==$("#menu li.active").find("a:last-child").not(".ignore-ele").attr("class") 
|| ($(window).scrollTop() + $(window).height() >= $(document).height() - 20))?($(".last-menuitem").length!=0 && $(".arrow").addClass('yellow'))
:($(".arrow").removeClass('yellow'))):
}

Thank you 谢谢

!_this.menuClicked

I am assuming menuClicked is a boolean representation of if a menu has been clicked or not. 我假设menuClicked是菜单是否被单击的布尔表示。 So what this does is say if a menu has been clicked return false. 因此,这就是说如果单击了菜单,则返回false。 Because the ! 因为! is a negation operator 是一个否定运算符

So if !_this.menuClicked == true then do this 因此,如果!_this.menuClicked == true则执行此操作

(($(".last-menuitem").attr("id") == $("#menu li.active").find("a:last-child").not(".ignore-ele").attr("class") 
|| 
$(".last-menuitem").find(".view-holder").attr("id") == $("#menu li.active").find("a:last-child").not(".ignore-ele").attr("class") 
|| 
($(window).scrollTop() + $(window).height() >= $(document).height() - 20))

(".last-menuitem").attr("id") == $("#menu li.active").find("a:last-child").not(".ignore-ele").attr("class") 

The first or checks if the lastMenuItems id equals the active menu items last child that doeesnt have the ignore else class on it. 第一个或检查lastMenuItems id是否等于活动菜单项的最后一个子级,该子级上不包含ignore else类。

if that is true we go to the nested turnary 如果是这样,我们转到嵌套的转台

($(".last-menuitem").length!=0 && $(".arrow").addClass('yellow'))

Which checks if the last-menuitem class has elements in it, then it makes the element with .arrow class have the yellow css styling. 它检查last-menuitem类中是否包含元素,然后使带有.arrow类的元素具有黄色的CSS样式。

If the first part of the nested turnery is false then we hit the second part 如果嵌套车床的第一部分是假的,那么我们打第二部分

$(".last-menuitem").find(".view-holder").attr("id") == $("#menu li.active").find("a:last-child").not(".ignore-ele").attr("class") 

which is if the lastmenuItem with the view-holder class's id equals the active menu items last chiled that doeesnt have the ignore else class on it. 这就是说,具有视图持有者类ID的lastmenuItem等于最后编译的活动菜单项,该菜单项上不包含ignore else类。

the do the previous mentioned true part. 做前面提到的真实部分。 If not do the third part of the nested turnery 如果不做,则进行嵌套车床的第三部分

($(window).scrollTop() + $(window).height() >= $(document).height() - 20)) if the scroll position + the height of the window is greater then the document height - 20 return true. ($(window).scrollTop() + $(window).height() >= $(document).height() - 20))如果滚动位置+窗口高度大于文档高度-20返回true。 and do the true part of the nested turnery. 并完成嵌套车床的真实部分。 else if all return false do the false part 否则,如果全部返回假,则做假部分

($(".arrow").removeClass('yellow'))):

If everything is false remove yellow from the element with the .arrow class. 如果一切都不正确,则从带有.arrow类的元素中删除黄色。 below is the turnery broken out to be readable 下面是车削技术,以方便阅读

!_this.menuclicked ?
(
     (
      $(".last-menuitem").attr("id") == $("#menu li.active").find("a:last-     
      child").not(".ignore-ele").attr("class") 
      ||    $(".last-menuitem").find(".view-holder").attr("id") == $("#menu 
             li.active").find("a:last-child").not(".ignore-ele").attr("class") 
       ||    ($(window).scrollTop() + $(window).height() >= $(document).height() 
             - 20)
      )
?  ($(".last-menuitem").length!=0 && $(".arrow").addClass('yellow'))
: ($(".arrow").removeClass('yellow'))
):

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

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