我不认为你可以document
用这样的.click
处理程序来定位。
与其让你可以从字面上单击任何地方来关闭 DIV,不如让它看起来像这样。在您希望能够关闭的DIV后面放置一个清晰的DIV并使其覆盖整个屏幕。然后你可以附加你的点击处理程序。
HTML:
<button>Click me to show the DIV</button>
<div class="container">
<div class="thediv">
<p>I'm the DIV</p>
</div>
</div>
JavaScript:
$(document).ready(function () {
var $button = $("button");
var $container = $("div.container");
$button.click(function () {
$container.show();
});
$container.click(function () {
$container.hide();
});
});
CSS:
div.container {
display: none;
position: fixed;
top: -5%;
left: -5%;
width: 110%;
height: 110%;
}
div.thediv {
position: absolute;
top: 30%;
left: 10%;
background-color: gray;
color: white;
padding-top: 1em;
border-radius: 5px;
width: 50%;
}
p {
background-color: black;
text-align: center;
padding: 2em;
}
出于演示目的,我使背景 DIV 在此 Fiddle中可见