日期:2014-05-16 浏览次数:20472 次
<script type="text/javascript">
function handle(e){
e=window.event||e;
var tag=e.srcElement||e.target;
if(tag.id!='testdiv'){
document.getElementById("testdiv").style.display='none';
}
}
window.onload=function(){
document.body.onclick=handle;
}
</script>
<div id="testdiv" style="width:600px;height:300px;border:1px solid #bfbfbf"></div>
------解决方案--------------------
//扩展非IE浏览器下的contains方法
if (window.Element) Element.prototype.contains = function (o) { if (this == o) return true; while (o = o.parentNode) if (o == this) return true; return false; }
var popID = 'xxxx';///////面板总容器ID
document.onclick = function (e) {
e = e || window.event;
var o = e.srcElement || o.target;
if (!document.getElementById(popID).contains(o)) //如果点击的对象不包含在面板中则隐藏
document.getElementById(popID).style.display = 'none';
}
------解决方案--------------------
思路不难,就是你用个大的DIV 包住你要关闭的DIV 当你点击小的DIV外面时候会调用大的DIV的单击事件,去判断小的DIV是否是显示的,如果是显示的就关闭它!