js关闭层问题,求大神指教啊,急~~~ 我页面上有一些隐藏层,点击某个按钮就弹出来,就是让它的display值从none变成block。比如表情,现在要求在点击除表情区域以外的网页位置,要使表情div关闭掉,这个js咋个处理啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <!doctype html><html> <head> <meta charset="gb2312" /> <title></title> <style> span { color:red; } #test { display:none; position:absolute; left:30px; top:30px; width:200px; border:1px solid red; background:#fff; outline:none; /* ff 去掉 test 聚焦后的 边框虚线 */ } </style> </head> <body> <span id="span">打开层</span> <a href="javascript:;" id="a">打开层</a> <div>1-1111111111</div> <div>1-1111111111</div> <div>1-1111111111</div> <div>1-1111111111</div> <div>1-1111111111</div> <div id="test"> 浮层,点击这个浮层以外的区域,都可以隐藏这个浮层 <div> <script> function $(o){return document.getElementById(o)} // ------------------- span -------------------- // 方法一: $('span').onclick = function(){ $('test').style.display = 'block'; $('test').tabIndex = -1; // 这里设置为-1,就不用在#test dom 中写 hidefocus="hidefocus" $('test').focus(); } $('test').onblur = function(){ $('test').style.display = 'none'; } // 方法二: /* $('span').onclick = function(e){ $('test').style.display = 'block'; e = e || window.event; if (e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble = true; } // 阻止事件冒泡 } document.documentElement.onclick = function(){ $('test').style.display = 'none'; } */ </script> </body></html>这里例了两个方法,还可以用事件委托 这位大神,多谢你的指教,不过用的你方式写出来,如果给隐藏层里面的内容加上超链接,在点击隐藏层里面超链接的内容时,就会触发blur时间,使隐藏层关闭,应该点击里面的超链接也不会触发blur事件才对。还有你的第二个方法我也试了,在点击隐藏层任何地方都会导致隐藏层关闭,不知道是哪儿出问题了 <!doctype html><html> <head> <meta charset="gb2312" /> <title></title> <style> span { color:red; } #test { display:none; position:absolute; left:30px; top:30px; width:200px; border:1px solid red; background:#fff; } </style> </head> <body> <span id="span">打开层</span> <div>1-1111111111</div> <div>1-1111111111</div> <div>1-1111111111</div> <div>1-1111111111</div> <div>1-1111111111</div> <div id="test"> <a href="#">123</a> 浮层,点击这个浮层以外的区域,都可以隐藏这个浮层 <div> <script> function $(o){return document.getElementById(o)} $('span').onclick = function(e){ $('test').style.display = 'block'; e = e || window.event; if (e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble = true; } } var odiv = $('test'); document.onclick = function(e){ e = e || window.event; var s = e.target || e.srcElement; if( e.srcElement ){ //ie if( !(s == odiv || odiv.contains(s)) ){ odiv.style.display = 'none'; } }else{ var res = odiv.compareDocumentPosition(s); if( !(s == odiv || res == 20 || res == 0) ){ odiv.style.display = 'none'; } } } </script> </body></html>再试试,应该ok了 Ajax调用,status偶尔会返回0的情况 jquery新手问题3 为什么ONMOUSEDOWN事件会跳转(求助~~) javascript 控制word文档的一个问题 图片无缝滚动有几种方法 树形结构的换行问题!急! 进入面页后如何设定他在20秒之后弹出窗口呢? 小白问一下gmail和新浪微博关于javascript的问题 怎么控制TEXT里的最长字符数? SOS!紧急向各位高手求救,分不够在加 window.open总是IE打开的问题 请教 点击回复 怎么replybox表格显示不出来呢?
<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
span {
color:red;
}
#test {
display:none;
position:absolute;
left:30px; top:30px;
width:200px;
border:1px solid red;
background:#fff;
outline:none; /* ff 去掉 test 聚焦后的 边框虚线 */
}
</style>
</head>
<body>
<span id="span">打开层</span>
<a href="javascript:;" id="a">打开层</a>
<div>1-1111111111</div>
<div>1-1111111111</div>
<div>1-1111111111</div>
<div>1-1111111111</div>
<div>1-1111111111</div>
<div id="test">
浮层,点击这个浮层以外的区域,都可以隐藏这个浮层
<div>
<script>
function $(o){return document.getElementById(o)}
// ------------------- span --------------------
// 方法一:
$('span').onclick = function(){
$('test').style.display = 'block';
$('test').tabIndex = -1; // 这里设置为-1,就不用在#test dom 中写 hidefocus="hidefocus"
$('test').focus();
}
$('test').onblur = function(){
$('test').style.display = 'none';
}
// 方法二:
/*
$('span').onclick = function(e){
$('test').style.display = 'block';
e = e || window.event;
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
}
// 阻止事件冒泡
}
document.documentElement.onclick = function(){
$('test').style.display = 'none';
}
*/
</script>
</body>
</html>
这里例了两个方法,还可以用事件委托
<!doctype html>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
span {
color:red;
}
#test {
display:none;
position:absolute;
left:30px; top:30px;
width:200px;
border:1px solid red;
background:#fff;
}
</style>
</head>
<body>
<span id="span">打开层</span>
<div>1-1111111111</div>
<div>1-1111111111</div>
<div>1-1111111111</div>
<div>1-1111111111</div>
<div>1-1111111111</div>
<div id="test">
<a href="#">123</a>
浮层,点击这个浮层以外的区域,都可以隐藏这个浮层
<div>
<script>
function $(o){return document.getElementById(o)}
$('span').onclick = function(e){
$('test').style.display = 'block';
e = e || window.event;
if (e.stopPropagation) {
e.stopPropagation();
} else {
e.cancelBubble = true;
} }
var odiv = $('test');
document.onclick = function(e){
e = e || window.event;
var s = e.target || e.srcElement;
if( e.srcElement ){ //ie
if( !(s == odiv || odiv.contains(s)) ){
odiv.style.display = 'none';
}
}else{
var res = odiv.compareDocumentPosition(s);
if( !(s == odiv || res == 20 || res == 0) ){
odiv.style.display = 'none';
}
}
}
</script>
</body>
</html>再试试,应该ok了