2016-07-27 6 views
-1

私はコードスニペットの下を見つけてそれを理解しようとしました。私は三項演算子を使用していて、menuclickでアクティブなクラスを追加していることを理解していますが、詳細な説明はわかります。Javascriptの三項ステートメントコードスニペットを理解する

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'))): 
} 

+1

正確にわからないことはありますか? – nicael

+0

私はアクティブなクラスを追加するときに、以下のステートメントを使用して理解したいと思います(($( "。last-menuitem")。attr( "id")== $( "#menu li.active") attr( "class") "は、"ここでは正確にやっています ".find(" a:last-child ")。not("。ignore-ele " – almondn

答えて

0

!_this.menuClicked

ありがとう私はmenuClickedメニューがクリックされたかどうのブール表現であると仮定しています。これは、メニューがクリックされてfalseを返す場合のことです。 !

ので!_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") 

最初または小切手をlastMenuItems idがアクティブなメニュー項目を無視していdoeesnt最後の子を等しい場合そうでなければクラスを作成します。

それは我々が最後のMenuItemクラスは、その中の要素を持っているかどうかを確認する、ネストされたturnary

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

に行く真であるならば、それは.arrowクラスを持つ要素は、黄色のCSSスタイルを持っています。ビュー・ホルダー・クラスのIDを持つlastmenuItemは、アクティブなメニュー項目が最後doeesntが持っているchiled等しい場合

ネストされたturneryの最初の部分がfalseの場合

は、我々は、第二部

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

を打ちます他のクラスを無視してください。

上記の真部分があります。ネストされたturnery

($(window).scrollTop() + $(window).height() >= $(document).height() - 20)) の3番目の部分をしない場合はスクロール位置+ウィンドウの高さは、文書の高さ、その後大きい場合 - 20リターン真。ネストされた刈り取りの真の部分を行います。 falseを返す場合はfalseを返します。

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

すべてがfalseの場合は、.arrowクラスの要素から黄色を削除します。 以下は、読みやすいように切り分けたものです。

!_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')) 
):