当我点击更多选项(例如 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/