效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200709172248415.gif)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<style type="text/css"> * {
margin: 0;
padding: 0
}
a {
display: inline-block;
width: 50px;
height: 50px;
background: #f40;
margin: 50px 0 0 50px;
}
div {
margin: 60px 0 0 50px;
padding: 20px;
outline: none;
border: 1px solid #39ee1a;
border-radius: 5px;
width: 200px;
}
</style>
</head>
<body>
<a id="search"></a>
<div id="searchBox"></div>
</body>
<script type="text/javascript">
window.onload = function () {
var obj = document.getElementById("searchBox");
var oBtn = document.getElementById("search");
searchBox.style.display = "none";
oBtn.onclick = function () {
obj.style.display = "block";
}
document.onclick = function (event) {
var e = event || window.event;
var aim = e.srcElement || e.target;
if (e.srcElement) {
var aim = e.srcElement;
if (aim != oBtn && aim != obj) {
obj.style.display = "none";
}
} else {
var aim = e.target;
if (aim != oBtn && aim != obj) {
obj.style.display = "none";
}
}
}
}
</script>
</html>