ユーザーが開いている状態以外でクリックしたときにドロップダウンメニューを非表示にしようとしています。メニューが開いているかどうかを判断するフラグisActive
を使用してから、ドキュメントにクリックイベントを追加して、開いている場合はメニューを非表示にし、伝播を停止しましたクリックするとメニューに表示されます。ただし、現在、ドロップダウン アンカー タグをクリックすると、ドキュメントのクリック イベントが発生します。誰でもこれを修正する方法をアドバイスできますか?
JS
//User profile share tooltip
$('.btn-social-share').on('click', function(e){
e.preventDefault();
if( !isActive ){
$('.social-share-options').show();
isActive = true;
} else {
$('.social-share-options').hide();
isActive = false;
}
});
/* Anything that gets to the document
will hide the dropdown */
$(document).on('click', function(){
if( isActive ){
$('.social-share-options').hide();
isActive = false;
}
});
/* Clicks within the dropdown won't make
it past the dropdown itself */
$('.social-share-options').click(function(e){
e.stopPropagation();
});