사용자가 ID = 'menubutton'요소를 클릭 한 경우를 제외하고 어떻게 메뉴를 숨 깁니까?본체에서 숨기기 exept item id = something
$('body').click(function(event) {
$('#menu').hide();
});
사용자가 ID = 'menubutton'요소를 클릭 한 경우를 제외하고 어떻게 메뉴를 숨 깁니까?본체에서 숨기기 exept item id = something
$('body').click(function(event) {
$('#menu').hide();
});
사용하지() 선택에 대한 일부 내용을 변경해야 할 수도 있습니다
$('body :not(#menubutton)').click(function(event) {
$('#menu').hide();
});
이것은 실제로는 비효율적입니다. 말 그대로 DOM의 모든 노드에 클릭 핸들러를 설정합니다 (단, # menubutton 요소 제외). – jmar777
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();
});
을하지 않는 선택기() 귀하의 경우
아무 것도 선택하지 않습니다. 현재'body' 태그는 제공된 선택자와 일치하지 않는 모든'body' 태그와 일치합니다. 그리고'body' 태그는 절대로 다른 div 내에 있지 않으므로 아무 것도하지 않습니다. – jmar777
* 메뉴 *, 어떤 메뉴? 더 설명해주세요 – Dhiraj