document.getElementById("but").onclick = function(e) {
showDropDown(this, e);
};
function showDropDown(element, e) {
element.onclick = function() {};
if (e.stopPropagation) e.stopPropagation(); // W3C model
else e.cancelBubble = true; // IE model
document.getElementById("window").style.display = "inline-block";
document.onclick = function(e) {
var ele = document.elementFromPoint(e.clientX, e.clientY);
if (ele == element) {
hideDropDown();
return;
}
do {
if (ele == document.getElementById("window")) return;
} while (ele = ele.parentNode);
hideDropDown(element);
};
}
function hideDropDown(element) {
document.onclick = function() {};
document.getElementById("window").style.display = "none";
element.onclick = function(e) {
showDropDown(this, e);
};
}
<input id="but" type="button" value="pressMe" />
<div id="window" style="display:none">popup</div>
错误:https ://www.dropbox.com/s/uzeiq6043rvueqf/Capture.PNG https://www.dropbox.com/s/w3rct18cumwva7m/bar3.png