2013-12-11 8 views
-3

div 영역 외부를 클릭하면 div를 숨기는 기능이 필요합니까?Div 요소를 바깥 쪽을 클릭하면 닫습니다.

내 사업부 : 가 나는 다음과 같은 기능을 사용하여 볼 수 있도록 Position: none에 사업부를 정의

<div id="TopBarBoxInfo1" onclick="showSerachOptions('BoxBox');" > 



</div> 

내 기능 :

function showSerachOptions(element){ 

var element = document.getElementById(element); 


// And then it will change what it is 
if(element.id == 'Box'){ 

    if(element.style.display == 'none'){ 

     element.style.display = 'block'; 

    } 
    else{ 

     element.style.display = 'none'; 

    } 
} 
} 

가 지금은 기능을해야합니다, div 영역 밖에서 마우스 포인터로 클릭하면 div를 닫을 수 있습니다. 초보자이기 때문에 솔루션을 자세하게 설명하십시오!

+0

가능 중복 될 것이다 (http://stackoverflow.com/questions/ 2868582/click-outside-menu-to-close-in-jquery) –

+0

이 질문은 영어로 작성되지 않았으므로 낯선 것으로 보입니다. – TCHdvlp

답변

0

등록 된 것은 귀하의 질문에 답변 할 규칙을 아는 것이 아니므로 항상 영어로 질문해야합니다.

var body = document.getElementsByTagName('body')[0]; 
body.click = function(e){ 
//here you can get e.target as the click-target-element or 
// e.srcElement(Microsoft) as the click-target-element 
//than you can handle what you want to do since e.target/srcElement will give you 
// an element (just like document.getElementById 
} 

가장 좋은 방법하지 않을 수 있습니다 .click 기억 - 또는 addEventListener은 [JQuery와에 닫으려면 메뉴 외부를 클릭]의 더 나은 AFAIK

관련 문제