点击div空白的地方 使div隐藏

zulowd 2014-02-10 03:19:16

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".btn").click(function(){
$(".xs_div").toggle();
})
});
</script>
<style>
*{
margin:0;
padding:0;
}
.div{
width:200px;
height:200px;
position:absolute;
top:50px;
left:100px;
background:#00BCF3;
display:none;
}
</style>
</head>

<body>
<input class="btn" type="button" value="点击"/>
<div class="xs_div"></div>
</body>
</html>


刚开始div是隐藏的 我点击按钮使div显示 那么怎么才能点击div以外空白的地方 使div隐藏
...全文
531 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
    if (window.HTMLElement && !HTMLElement.prototype.contains) HTMLElement.prototype.contains = function (o) { if (o == this) return true; while (o = o.parentNode) if (o == this) return true; return false; }
    $(document).ready(function () {
        $(".btn").click(function (e) {
            e.stopPropagation();
            $(".xs_div").toggle();
        })
    }).click(function (e) {
        if (!$(".xs_div")[0].contains(e.target)) $(".xs_div").hide();
    });
</script>
<style>
*{
    margin:0;
    padding:0;
    }
.xs_div{
    width:200px;
    height:200px;
    position:absolute;
    top:50px;
    left:100px;
    background:#00BCF3;
    display:none;
    }
</style>
</head>
 
<body>
<input class="btn" type="button" value="点击"/>
<div class="xs_div"></div>
</body>
</html>
  • 打赏
  • 举报
回复
你的代码本身存在一个问题,样式应该是 div 而不是.div 因为没有class为div的标签

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $(".btn").click(function(ev){
			$(".xs_div").show();
			return false;
		})	
	$(".xs_div").click(function(ev){
		return false;
	})
	$(document).click(function(){
		$(".xs_div").hide();
	});
});
</script>
<style>
*{
	margin:0;
	padding:0;
	}
div{
	width:200px;
	height:200px;
	position:absolute;
	top:50px;
	left:100px;
	background:#00BCF3;
	display:none;
	}
</style>
</head>

<body>
<input class="btn" type="button" value="点击"/>
<div class="xs_div"></div>
</body>
</html>

87,997

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧