当点击DIV以外的地方,隐藏该DIV,求jquery或js方法。 如题,求助各位达人,先谢了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 第一步:在js中定义一个变量用来保存鼠标是否在div中这个状态:var isOut=true;(默认不再区域中)第二步:在div中加上onmouseover和onmouseout事件,用来控制变量isOut:第三步:在显示div的事件中加入取消事件冒泡的语句:(因为这个事件也会触发document.onmousedown)event.cancelBubble=true;第四步:为document添加onmousedown事件:document.onmousedown=function(){ var codediv=document.getElementById("codediv"); if(codediv.style.display!="none" && isOut){ codediv.style.display!="none"; }} <!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><style type="text/css">#pop{border:#000; background-color:#CCC; position:absolute; left:50px; top:50px;width:200px;height:500px; display:none}#btn{color:#f00}</style><script type="text/javascript">function $(id){return document.getElementById(id);}window.onload = function(){ document.onclick = function(e){ $("pop").style.display = "none"; } $("btn").onclick = function(e){ $("pop").style.display = "block"; e = e||event; stopFunc(e); } $("pop").onclick = function(e){ e = e||event; stopFunc(e); }}function stopFunc(e){ e.stopPropagation?e.stopPropagation():e.cancelBubble = true; }</script></head><body> <span id="btn">打开层</span><div>aaaaaaaaaaaa</div><div>aaaaaaaaaaaa</div><div>aaaaaaaaaaaa</div><div>aaaaaaaaaaaa</div><div>aaaaaaaaaaaa</div><div>aaaaaaaaaaaa</div><div id="pop"> 浮层,点击这个浮层以外的区域,都可以隐藏这个浮层,怎么做啊,多谢了 <p><a href="http://www.baidu.com" target="_blank">最主要的是点这个div里面的链接,div照样不隐藏</a></p></div></body></html> EXTjs事件 这样做不就跨域了吗? 一圆角幻灯片怎么实现 大家帮我看看这段JS播放器的代码?看看能不能修改。。。 一个有关于js调用java的问题 为什么<script>标记不能放到 Span 内??? serializable 是什么来的 用OPEN方法怎么在框架中打开一个页 关于<input type=file>的一个问题 javascript做出的一个弹出式菜单放在框架结构中,菜单被遮挡问题? 新手加入JS军团!混个脸熟!以后问题还请大虾们多多帮忙 网页应用.转发到QQ空间.新浪微博,人人网.这些功能怎么添加到自己站上
第二步:在div中加上onmouseover和onmouseout事件,用来控制变量isOut:
第三步:在显示div的事件中加入取消事件冒泡的语句:(因为这个事件也会触发document.onmousedown)
event.cancelBubble=true;
第四步:为document添加onmousedown事件:
document.onmousedown=function(){
var codediv=document.getElementById("codediv");
if(codediv.style.display!="none" && isOut){
codediv.style.display!="none";
}
}
<!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>
<style type="text/css">
#pop{border:#000; background-color:#CCC; position:absolute; left:50px; top:50px;width:200px;height:500px; display:none}
#btn{color:#f00}
</style>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
window.onload = function(){
document.onclick = function(e){
$("pop").style.display = "none";
}
$("btn").onclick = function(e){
$("pop").style.display = "block";
e = e||event; stopFunc(e);
}
$("pop").onclick = function(e){
e = e||event; stopFunc(e);
}
}
function stopFunc(e){
e.stopPropagation?e.stopPropagation():e.cancelBubble = true;
}
</script>
</head>
<body>
<span id="btn">打开层</span>
<div>aaaaaaaaaaaa</div>
<div>aaaaaaaaaaaa</div>
<div>aaaaaaaaaaaa</div>
<div>aaaaaaaaaaaa</div>
<div>aaaaaaaaaaaa</div>
<div>aaaaaaaaaaaa</div><div id="pop">
浮层,点击这个浮层以外的区域,都可以隐藏这个浮层,怎么做啊,多谢了
<p><a href="http://www.baidu.com" target="_blank">最主要的是点这个div里面的链接,div照样不隐藏</a></p>
</div>
</body>
</html>