日期:2014-05-16  浏览次数:20398 次

弹出层隐藏的问题
有一个输入文本框,点击以后会弹出一个div层(均已实现),现在想在页面上其他地方(该div层和文本框上不能触发)点击的时候关闭弹出的div层,这个用js怎么实现?jquery 
                      $("div层id").blur (绑定click)
没有效果。

怎么实现这个效果

------解决方案--------------------
    $(document).click(function (e) {
        var o = e.target; //获取当前点击对象,下面执行判断不是当前文本框并且不是弹出层
        if (o[0] != $('#文本框ID')[0] && o.closest('#弹出层的ID').size() == 0) $('#弹出层的ID').hide();
    });

------解决方案--------------------
<!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>
<script type="text/javascript">
function init(){
var input=document.getElementById("test");
input.onclick=function(e){
var styles={
position:"absolute",
height:"20px",
top:"0px",
width:"100px",
left:"0px",
backgroundColor:"red"
}
var div=document.createElement("div");
for(var i in styles){
div.style[i]=styles[i];
}
div.setAttribute("id","div");
document.body.appendChild(div);
var a=e
------解决方案--------------------
window.event;
if(e.cancelBubble){
e.cancelBubble=true;
}else if(a.stopPropagation){
a.stopPropagation();
}
}
document.onclick=function(){
var div=document.getElementById("div");
if(div){
document.body.removeChild(div);
}
}
}
window.onload=init;
</script>
</head>

<body>
<input type="button" value="test" id="test">
</body>
</html>
给document加个click事件  这样当点击任何元素时都会冒泡到document上,把点击不需要删除div的元素阻止冒泡试试可以不