教程集 www.jiaochengji.com
教程集 >  jQuery  >  jquery 教程  >  正文 jQuery点击区域外消失

jQuery点击区域外消失

发布时间:2016-09-08   编辑:jiaochengji.com
教程集为您提供jQuery点击区域外消失等资源,欢迎您收藏本站,我们将为您提供最新的jQuery点击区域外消失资源

需要冒泡添加e.stopPropagation


<!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>box - mming</title>
<meta name="keywords" content="" />
<meta name="description" content="" />

<style type="text/css">
.box {width:100px; height:100px; background:#f00;}
</style>

<script type="text/javascript" src="https://ajax.useso.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$(document).click(function() {
$('.box').hide();
})
$('.box').click(function(event) {
event.stopPropagation();
})
});
</script>
</head>

<body>

<div>box</div>

</body>
</html>

JS


<!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=GB2312" />
<title>点击一个连接显示层</title>
<style type="text/css">
#myDiv{
border:1px solid #000000;
width:200px;
height:100px;
background:#cccccc;
}
</style>
<script language="JavaScript" type="text/javascript">
function addListener(element, e, fn) {
if (element.addEventListener) {
element.addEventListener(e, fn, false);
} else {
element.attachEvent("on" + e, fn);
}
}
addListener(document, "click",
function(evt) {
var evt = window.event ? window.event: evt,
target = evt.srcElement || evt.target;
if (target.id == "showDiv") {
document.getElementById("myDiv").style.display = "";
return;
} else {
while (target.nodeName.toLowerCase() != "div" && target.nodeName.toLowerCase() != "html") {
target = target.parentNode;
}
if (target.nodeName.toLowerCase() == "html") {
document.getElementById("myDiv").style.display = "none";
}

}
})
</script>
</head>
<body>
<div id="myDiv" style="display:none;">就等你包养了</div>
<input type="button" value="快来摸我" id="showDiv" />
</body>
</html>

您可能感兴趣的文章:
点击弹出层外区域关闭弹出层jquery特效示例
jquery实现点击消失的代码
jQuery点击区域外消失
JQuery实现点击div以外的位置隐藏该div窗口
基于jquery实现的服务器验证控件的启用和禁用代码
jquery 半透明遮罩效果示例
Windows7系统语言栏图标在任务栏失败了怎么办
jquery 点击按钮弹出层 点击空白处隐藏层的实现代码
jQuery点击div其他地方隐藏div
基于jQuery的可以控制左右滚动及自动滚动效果的代码

[关闭]
~ ~