请问如果有个弹出层 我想第一次点击一个按钮让他显示 然后我点击非他的任何一个元素让他消失 当然点击它本身他不消失 用jquery或者js咋做 最好能给出具体的代码 或者例子 用jquery或者javascript 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我的jquery是这么写的 但是第一下点击是出来了 但是再点击整个页面都没了function divHide() { $(".divHide").each(function () { $("body").not(this).click(function () { if ($(this).is(":visible")) { $(this).hide(); } }); });}请高手指点 <div id="scs" style="width:200px;height:200px;border: 1px solid red;display: none"></div> <input type="button" value="显示" id="scscms" /><script type="text/javascript">$(function(){ $("#scscms,#scs").click(function(e){ $("#scs").show(); e.stopPropagation();//阻止冒泡 }); $(document).click(function(e){ $("#scs").hide(); e.stopPropagation(); })})</script> 用原生JS写了个简单的DEMO:<!DOCTYPE html><html><head> <meta charset="utf-8"/> <title>Demo</title> <style type="text/css"> body{ margin:0; padding:0; height:600px; } #test{ width:400px; height:200px; border:1px solid #F60; background:#EEE; position:absolute; z-index:2; display:none; } </style></head><body> <div id="test">Layer</div> <input type="button" id="showLayer" value="Show"/> <script> function getCSS(obj,prototype){ if(obj.currentStyle){ return obj.currentStyle[prototype]; } else{ return document.defaultView.getComputedStyle(obj,null)[prototype]; } } var showBtn=document.getElementById("showLayer"), layer=document.getElementById("test"); showBtn.onclick=function(e){ layer.style.left = ((window.innerWidth || document.body.clientWidth) - parseInt(getCSS(layer,"width")))/2 + "px"; layer.style.top = ((window.innerHeight || document.body.clientHeight) - parseInt(getCSS(layer,"height")))/2 + "px"; layer.style.display="block"; if(e.cancelBubble) e.cancelBubble = true; else e.stopPropagation(); } layer.onclick=function(e){ e= e || window.event; if(e.cancelBubble) e.cancelBubble = true; else e.stopPropagation(); } document.body.onclick=function(e){ layer.style.display = "none"; } </script></body></html> jQuery怎么去除一个节点下所有节点的样式 web服务器端的JS脚本调用exe文件问题? 混淆的js代码 是如何在ie中进行解析 发现使用JS操作外部样式表的局限性 根据身份证号获取省份城市信息 请问,javascript 中的 bookmarklet 是什么? 如何通过窗口的名字来得到这个窗口的对象? 求只能输入汉字和标点符号的正则表达式 XMLDOM~~~~~ javascript中如何判断一个控件是否存在? 鼠标 停靠首先是把小图放大 定位如何实现? js的程序参数传不进来怎么回事呢?
$(".divHide").each(function () {
$("body").not(this).click(function () {
if ($(this).is(":visible")) {
$(this).hide();
}
});
});
}
请高手指点
<input type="button" value="显示" id="scscms" />
<script type="text/javascript">
$(function(){
$("#scscms,#scs").click(function(e){
$("#scs").show();
e.stopPropagation();//阻止冒泡
});
$(document).click(function(e){
$("#scs").hide();
e.stopPropagation();
})
})
</script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Demo</title>
<style type="text/css">
body{
margin:0;
padding:0;
height:600px;
}
#test{
width:400px;
height:200px;
border:1px solid #F60;
background:#EEE;
position:absolute;
z-index:2;
display:none;
}
</style>
</head>
<body>
<div id="test">Layer</div>
<input type="button" id="showLayer" value="Show"/>
<script>
function getCSS(obj,prototype){
if(obj.currentStyle){
return obj.currentStyle[prototype];
} else{
return document.defaultView.getComputedStyle(obj,null)[prototype];
}
} var showBtn=document.getElementById("showLayer"),
layer=document.getElementById("test");
showBtn.onclick=function(e){
layer.style.left = ((window.innerWidth || document.body.clientWidth) - parseInt(getCSS(layer,"width")))/2 + "px";
layer.style.top = ((window.innerHeight || document.body.clientHeight) - parseInt(getCSS(layer,"height")))/2 + "px";
layer.style.display="block";
if(e.cancelBubble)
e.cancelBubble = true;
else
e.stopPropagation();
} layer.onclick=function(e){
e= e || window.event;
if(e.cancelBubble)
e.cancelBubble = true;
else
e.stopPropagation();
} document.body.onclick=function(e){
layer.style.display = "none";
}
</script>
</body>
</html>