2011-06-27 9 views
0

私はメニュープロファイルのリンクを持っています。それは 'ユーザーボタン'と呼ばれるクラスを持っています。ユーザーがクリックすると、jqueryは別のクラスをulリストに追加して表示します。他の要素のjQuery onclick

$(".user-button").click(function() { 
    if(!$(".arrow").hasClass('toggle')) { 
     $(".arrow").addClass('toggle'); 
     $(".profile-action").addClass('display-it'); 
    } else { 
     $(".arrow").removeClass('toggle'); 
     $(".profile-action").removeClass('display-it'); 
    } 
}); 

画像参照:http://cl.ly/0R1u3A0h341s2w3a3Y3L

どのように私はこのような「.profileを-アクション」閉じるための体のような他の要素でユーザーのクリックを作るのですか? 例:facebook modaless

ありがとう。

答えて

1

はあなたのコードにこれを追加します。

$('html').click(function() { 
    $(".arrow").removeClass('toggle'); 
    $(".profile-action").removeClass('display-it'); 
}); 

そしてpropgationを防ぐために、あなたの現在のコードを変更:

$(".user-button").click(function(event) { 
    event.stopPropagation(); 
    if(!$(".arrow").hasClass('toggle')) { 
     $(".arrow").addClass('toggle'); 
     $(".profile-action").addClass('display-it'); 
    } else { 
     $(".arrow").removeClass('toggle'); 
     $(".profile-action").removeClass('display-it'); 
    } 
}); 
+0

あなたは素晴らしいです!それは今動作するhttp://jsfiddle.net/LyXc2/ – aje