隐藏在正文单击EXPT项目ID =某物

Hide on body click exept item id = something

本文关键字:ID 某物 项目 EXPT 正文 单击 隐藏      更新时间:2023-09-26

如何隐藏菜单,除非用户单击了ID = 'menubutton'的元素?

$('body').click(function(event) {
    $('#menu').hide();
    });

使用 not() 选择器

$('body :not(#menubutton)').click(function(event) {
    $('#menu').hide();
});

http://api.jquery.com/not-selector/

使用 target 元素。

$('body').click(function(event) {
    // If the element clicked doesn't have the id "menubutton"
    if ( $(event.target).attr( 'id' ) !== 'menubutton' ) {
        $('#menu').hide();
    }
});
$('body').click(function(event) {
    // don't hide if the clicked element was #menubutton,
    // or any element within #menubotton
    if (!$(event.target).closest('#menubutton').length) {
        $('#menu').hide();
    }
});
$('body :not(div #menubutton)').click(function(event) {
   $('#menu').hide();
});

jQuery Not()

not() 的选择器可能需要对你的情况进行一些更改