jquery - onclick 切换下拉按钮

标签 jquery onclick toggle

当我点击更多选项(例如 Google)时,我需要一个下拉菜单。它正在工作,但是当我在 div 外部单击时它处于非事件状态。我想在单击 div 外部时切换菜单。

我的 JavaScript:

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

最佳答案

从中得到一个想法answer

$(document).mouseup(function (e)
{
    var container = $("#other");

    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {
        container.toggle();
    }
});

我的想法,它不应该切换,最好是显示/隐藏机制。

关于jquery - onclick 切换下拉按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18017088/

相关文章:

javascript - 如何在打印前动态更改文档的方向

javascript - Perl - 向主机提交 Javascript 操作

JQuery .each() 切换隐藏元素

javascript - 计算可观察问题

javascript - 使用 PHP 将用户输入和时间记录到文本文件

javascript - javascript中动态创建的按钮中的Onclick事件

jquery - 在单个链接上切换类?

html - html5 音频的切换开关

php - 两个数组的 Google 散点图

javascript - 可点击的表格行javascript