var box = document.getElementById("box"); var btn = document.getElementById("btn"); btn.onclick = function(event){ event.stopPropagation(); if(box.style.display == "none"){ box.style.display = "block"; }else{ box.style.display = "none"; } }; window.onclick = function(){ box.style.display = "none" };
前提是要给div加一个行内的display:none;