我无法使用按钮关闭下拉菜单,但当我点击页面外部时可以关闭它。
var $menu = $('.menu');
$('.toggle').click(function () {
$menu.toggle();
});
$(document).mouseup(function (e) {
if (!$menu.is(e.target) // if the target of the click isn't the container...
&& $menu.has(e.target).length === 0) // ... nor a descendant of the container
{
$menu.hide();
}
});
请大家帮个忙。
.dropdown
元素或其后代是否为目标,则应该可以工作。(因为这样你的按钮就被包括在内了。) - misorudeevent.stopPropagation();
@john 看看我的回答。 - Sumesh TG