如何实现一个div的隐藏与显示?
页面上有一个按钮和一个div
div默认隐藏,当按按钮的时候div居中显示,后页面变灰。
按确定后,div重新隐藏,后页面显示。
如果先设置div的display:none
document.getElementById("id1")就找不到id1这个div
怎样实现一个div的隐藏与显示?
[解决办法]
刚好我昨天帮别人做了两个
代码给你 自己看吧
<style type="text/css"><!--.menu{ border:red 1px solid; position:absolute; width:100px; height:auto !important; height:100%; top:42px !important; top: 41px; z-index:99; display:none; border:#ccc 1px solid; color:#ccc;}ul{ text-align:left;}ul li{ display:block; list-style-type: none; float:left; width:100px; height:25px; border-right:red 1px solid; border-bottom:red 1px solid; border-top:red 1px solid; line-height:25px;}ul li#first{ border-left:red 1px solid;} ul li a{ margin-left:30px; text-decoration:none; color:#000; }ul li a:hover{ margin-left:30px; text-decoration:none; color:#CCC; }--></style><script language="javascript">function $(id){ return document.getElementById(id);}function Show(arg){ $(arg).style.display="block";}function Hide(arg){ $(arg).style.display="none";}</script> <ul> <li onmouseover="Show('menu1')" onmouseout="Hide('menu1')" id="first"><a href="#">menu1</a><br /> <div id="menu1" class="menu" ><!--级联菜单--> <table> <tr><td>menu1_1</td></tr> <tr><td>menu1_1</td></tr> <tr><td>menu1_1</td></tr> </table> </div> </li> <li onmouseover="Show('menu2')" onmouseout="Hide('menu2')"><a href="#">menu2</a><br /> <div id="menu2" class="menu"><!--级联菜单--> <table> <tr><td>menu2_1</td></tr> <tr><td>menu2_1</td></tr> <tr><td>menu2_1</td></tr> </table> </div></li> <li onmouseover="Show('menu3')" onmouseout="Hide('menu3')"><a href="#">menu3</a><br /> <div id="menu3" class="menu"><!--级联菜单--> <table> <tr><td>menu3_1</td></tr> <tr><td>menu3_1</td></tr> <tr><td>menu3_1</td></tr> </table> </div> </li> <li onmouseover="Show('menu4')" onmouseout="Hide('menu4')"><a href="#">menu4</a><br /> <div id="menu4" class="menu"><!--级联菜单--> <table> <tr><td>menu4_1</td></tr> <tr><td>menu4_1</td></tr> <tr><td>menu4_1</td></tr> </table> </div> </li> </ul> <p> </p> <p>实现边框不重叠的效果还有一种办法就是marig属性 加上背景 有很细的裂缝 可以看成是边框</p>
[解决办法]
IE6 FF2测试通过
<!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=gb2312" />
<title></title>
<STYLE>
#login{
position: relative;
display: none;
top: 20px;
left: 30px;
background-color: #ffffff;
text-align: center;
border: solid 1px;
padding: 10px;
z-index: 1;
}
</STYLE>
<script type="text/javascript">
var W = screen.width;//取得屏幕分辨率宽度
var H = screen.height;//取得屏幕分辨率高度
function M(id){
return document.getElementById(id);//用M()方法代替document.getElementById(id)
}
function MC(t){
return document.createElement(t);//用MC()方法代替document.createElement(t)
};
//判断浏览器是否为IE
function isIE(){
return (document.all && window.ActiveXObject && !window.opera) ? true : false;
}
//取得页面的高宽
function getBodySize(){
var bodySize = [];
with(document.documentElement) {
bodySize[0] = (scrollWidth>clientWidth)?scrollWidth:clientWidth;//如果滚动条的宽度大于页面的宽度,取得滚动条的宽度,否则取页面宽度
bodySize[1] = (scrollHeight>clientHeight)?scrollHeight:clientHeight;//如果滚动条的高度大于页面的高度,取得滚动条的高度,否则取高度
}
return bodySize;
}
//创建遮盖层
function popCoverDiv(){
if (M("cover_div")) {
//如果存在遮盖层,则让其显示
M("cover_div").style.display = 'block';
} else {
//否则创建遮盖层
var coverDiv = MC('div');
document.body.appendChild(coverDiv);
coverDiv.id = 'cover_div';
with(coverDiv.style) {
position = 'absolute';
background = '#CCCCCC';
left = '0px';
top = '0px';
var bodySize = getBodySize();
width = bodySize[0] + 'px'
height = bodySize[1] + 'px';
zIndex = 0;
if (isIE()) {
filter = "Alpha(Opacity=60)";//IE逆境
} else {
opacity = 0.6;
}
}
}
}
//让登陆层显示为块
function showLogin()
{
var login=M("login");
login.style.display = "block";
}
//设置DIV层的样式
function change(){
var login = M("login");
login.style.position = "absolute";
login.style.border = "1px solid #CCCCCC";
login.style.background ="#F6F6F6";
var i=0;
var bodySize = getBodySize();
login.style.left = (bodySize[0]-i*i*4)/2+"px";
login.style.top = (bodySize[1]/2-100-i*i)+"px";
login.style.width = i*i*4 + "px";
login.style.height = i*i*1.5 + "px";
popChange(i);
}
//让DIV层大小循环增大
function popChange(i){
var login = M("login");
var bodySize = getBodySize();
login.style.left = (bodySize[0]-i*i*4)/2+"px";
login.style.top = (bodySize[1]/2-100-i*i)+"px";
login.style.width = i*i*4 + "px";
login.style.height = i*i*1.5+ "px";
if(i<=10){
i++;
setTimeout("popChange("+i+")",10);//设置超时10毫秒
}
}
//打开DIV层
function open()
{
change();
showLogin();
popCoverDiv()
void(0);//不进行任何操作,如:<a href="#">aaa</a>
}
//关闭DIV层
function close(){
M('login').style.display = 'none';
M("cover_div").style.display = 'none';
void(0);
}
</script>
</head>
<body>
<a href="javascript:open();">登陆</a>
<div id="login">
<span>用户登陆</span>
<div id="panel">
用户名:<input type="text" size="20" />
密码: <input type="password" size="20">
<input type="checkbox" value="登录" />
</div>
<input type="button" value="提交" />
<a href="javascript:close();">关闭</a>
</div>
</body>
</html>
------解决方案--------------------
转载网上的
<!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>
<title> </title>
<style>
html,body{font-size:12px;margin:0px;height:100%;}
.mesWindow{border:#666 1px solid;background:#fff;}
.mesWindowTop{border-bottom:#eee 1px solid;margin-left:4px;padding:3px;font-weight:bold;text-align:left;font-size:12px;}
.mesWindowContent{margin:4px;font-size:12px;}
.mesWindow .close{height:15px;width:28px;border:none;cursor:pointer;text-decoration:underline;background:#fff}
</style>
<script>
var isIe=(document.all)?true:false;
//设置select的可见状态
function setSelectState(state)
{
var objl=document.getElementsByTagName('select');
for(var i=0;i <objl.length;i++)
{
objl[i].style.visibility=state;
}
}
function mousePosition(ev)
{
if(ev.pageX || ev.pageY)
{
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
//弹出方法
function showMessageBox(wTitle,content,pos,wWidth)
{
closeWindow();
var bWidth=parseInt(document.documentElement.scrollWidth);
var bHeight=parseInt(document.documentElement.scrollHeight);
if(isIe){
setSelectState('hidden');}
var back=document.createElement("div");
back.id="back";
var styleStr="top:0px;left:0px;position:absolute;background:#666;width:"+bWidth+"px;height:"+bHeight+"px;";
styleStr+=(isIe)?"filter:alpha(opacity=40);":"opacity:0.40;";
back.style.cssText=styleStr;
document.body.appendChild(back);
var mesW=document.createElement("div");
mesW.id="mesWindow";
mesW.className="mesWindow";
mesW.innerHTML=" <div class='mesWindowTop'> <table width='100%' height='100%'> <tr> <td>"+wTitle+" </td> <td style='width:1px;'> <input type='button' onclick='closeWindow();' title='关闭窗口' class='close' value='关闭' /> </td> </tr> </table> </div> <div class='mesWindowContent' id='mesWindowContent'>"+content+" </div> <div class='mesWindowBottom'> </div>";
styleStr="left:"+(((pos.x-wWidth)>0)?(pos.x-wWidth):pos.x)+"px;top:"+(pos.y)+"px;position:absolute;width:"+wWidth+"px;";
mesW.style.cssText=styleStr;
document.body.appendChild(mesW);
}
function showBackground(obj,endInt)
{
obj.filters.alpha.opacity+=1;
if(obj.filters.alpha.opacity <endInt)
{
setTimeout(function(){showBackground(obj,endInt)},8);
}
}
//关闭窗口
function closeWindow()
{
if(document.getElementById('back')!=null)
{
document.getElementById('back').parentNode.removeChild(document.getElementById('back'));
}
if(document.getElementById('mesWindow')!=null)
{
document.getElementById('mesWindow').parentNode.removeChild(document.getElementById('mesWindow'));
}
if(isIe){
setSelectState('');}
}
//测试弹出
function testMessageBox(ev)
{
var objPos = mousePosition(ev);
messContent=" <div style='padding:20px 0 20px 0;text-align:center'>消息正文 </div>";
showMessageBox('窗口标题',messContent,objPos,350);
}
</script>
</head>
<body>
<div style="padding:20px">
<div style="text-align:left";> <a href="#none" onclick="testMessageBox(event);">弹出窗口 </a> </div>
<div style="text-align:left;padding-left:20px;padding-top:10px";> <select> <option>下拉 </option> </select>弹出窗口时会将其隐藏,关闭时会让其显示,目的是在IE中防止弹出的DIV挡不住下拉框 </div>
<div style="text-align:center";> <a href="#none" onclick="testMessageBox(event);">弹出窗口 </a> </div>
<div style="text-align:right";> <a href="#none" onclick="testMessageBox(event);">弹出窗口 </a> </div>
</div>
</body>
</html>
本文来自: 脚本之家(www.jb51.net) 详细出处参考:http://www.jb51.net/article/12462.htm