js点击出现场层层外点击层消失的代码
点击一个连接显示层~点击其他区域隐藏 练习 by 我们
#myDiv{
border:1px solid #000000;
width:200px;
height:100px;
background:#cccccc;
}
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";
}
}
})
我们
[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]