2012-05-16 3 views

답변

2

사용하지() 선택에 대한 일부 내용을 변경해야 할 수도 있습니다

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

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

+1

이것은 실제로는 비효율적입니다. 말 그대로 DOM의 모든 노드에 클릭 핸들러를 설정합니다 (단, # menubutton 요소 제외). – jmar777

1

target 요소를 사용하십시오.

$('body').click(function(event) { 
    // If the element clicked doesn't have the id "menubutton" 
    if ($(event.target).attr('id') !== 'menubutton') { 
     $('#menu').hide(); 
    } 
}); 
1
$('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(); 
    } 
}); 
1
$('body :not(div #menubutton)').click(function(event) { 
    $('#menu').hide(); 
}); 

jQuery Not()

을하지 않는 선택기() 귀하의 경우

+0

아무 것도 선택하지 않습니다. 현재'body' 태그는 제공된 선택자와 일치하지 않는 모든'body' 태그와 일치합니다. 그리고'body' 태그는 절대로 다른 div 내에 있지 않으므로 아무 것도하지 않습니다. – jmar777

관련 문제