2

Google などのその他のオプションをクリックすると、ドロップダウン メニューが必要になります。機能していますが、divの外側をクリックすると非アクティブになります。divの外側をクリックしたときにメニューを切り替えたい。

私のJavaScript:

$('#other').click(function() {
    $(this).toggleClass('active');
    $(this).next('.dropdown').toggle();
    return false;
});
$('.dropdown a').click(function() {
    return false;
});
4

2 に答える 2

0

document.click にサブスクライブして、イベント ハンドラーでメニューを閉じることができます。ドロップダウン メニュー内をクリックしてもイベントが閉じないように、ドロップダウンからのイベントの伝播を停止します。

$('.dropdown').click(function (event) {
    event.stopPropagation();
    return false;
});

$(document).click(function (event) {
    if ($('.dropdown').is(':visible')) {
        $('.dropdown').toggle();
        $('#other').toggleClass('active');
    }
});

あなたのフィドルが更新されました。

于 2013-08-02T12:53:57.820 に答える