日期:2014-05-19  浏览次数:20458 次

【问题】页面上有个DIV层,当点击页面其它位置的时候,隐藏DIV
页面上有个DIV层,当点击页面其它位置的时候(及点击DIV以外部分)隐藏DIV。
这段代码应该怎么写?
谢谢

------解决方案--------------------
script> document.onclick = function() { if(event.srcElement != document.getElementById( "div1 ")) { document.getElementById( "div1 ").style.display = "none "; } } </script>
------解决方案--------------------
function showDiv (_show)
{

if (_show == true)
{
var alertFram = document.createElement( "DIV ");
alertFram.id= "busy ";
alertFram.style.position = "absolute ";
alertFram.style.left = "95% ";
alertFram.style.top = "10% ";
alertFram.style.marginLeft = "-225px " ;
alertFram.style.marginTop = -75+document.documentElement.scrollTop+ "px ";
alertFram.style.width = "450px ";
alertFram.style.height = "150px ";
alertFram.style.background = "#ccc ";
alertFram.style.textAlign = "center ";
alertFram.style.lineHeight = "150px ";
alertFram.style.zIndex = "10001 ";

var strHtml = " <font color=red > <strong> Loading... </strong> </font> ";
alertFram.innerHTML = strHtml;
document.body.appendChild(alertFram);
}
else
{
try
{
document.body.removeChild(busy);
}
catch(e)
{}
}
}


======================================================

showDiv(true) //显示div
showDiv(false) //隐藏div
------解决方案--------------------
顶cpp2017(慕白兄)
------解决方案--------------------
.div_hide
{
display = "none ";
}


------解决方案--------------------
“其它位置(及DIV以外部分)”较难从技术上搞清楚。
------解决方案--------------------
//借用墓白兄的代码
document.onclick = function()
{
if(event.srcElement != document.getElementById( "div1 "))
{
document.getElementById( "div1 ").style.display = "none ";
}
}

//把document.getElementById( "div1 ")的div1改成下拉箭头的id就好了
------解决方案--------------------
//借用墓白兄的代码
//这个应该会好点
document.onclick = function()
{
if(event.srcElement != document.getElementById( "div1 ") || event.srcElement != document.getElementById( "btnid "))
{
document.getElementById( "div1 ").style.display = "none ";
}
}

------解决方案--------------------
幕白的可以..