打开关闭DIV窗口的简单例子

看着别人可以打开或关闭DIV窗口,感觉比较好奇是怎么实现的,今天也稍微研究了一下。
html 代码
 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>打开关闭DIV例子</title>  
  6. <style type="text/css">  
  7. #div1 {  
  8.     margin: 10px auto;  
  9.     width: 300px;  
  10.     height: 300px;  
  11.     border: 2px #cccccc solid;  
  12. }  
  13.   
  14. #div2 {  
  15.     float: right;  
  16.     background-color: yellow;  
  17.     cursor: default;  
  18. }  
  19.   
  20. #div3 {  
  21.     clear: both;  
  22.     margin: 50px auto;  
  23. }  
  24. </style>  
  25. <script type="text/javascript">  
  26.     function openWin(){  
  27.         if(!document.getElementById("div1")){  
  28.             var div1 = document.createElement("div");  
  29.             div1.id = "div1";  
  30.             document.body.appendChild(div1);  
  31.               
  32.             var div2 = document.createElement("div");  
  33.             div2.id="div2";  
  34.             div2.onclick = closeWin;  
  35.             div2.onmouseover=overDIV2;  
  36.             div2.onmouseout=outDIV2;  
  37.             div2.innerHTML = "DEL";  
  38.             div1.appendChild(div2);  
  39.               
  40.             var div3 = document.createElement("div");  
  41.             div3.id="div3";  
  42.             div3.innerHTML="这是我们能看见的窗口,点一下DEL就消失了!";  
  43.             div1.appendChild(div3);  
  44.         }  
  45.     }  
  46.       
  47.     function closeWin(){  
  48.         var div=document.getElementById("div1");  
  49.         if(div != null){  
  50.             div.parentNode.removeChild(div);  
  51.         }  
  52.     }  
  53.       
  54.     function overDIV2(){  
  55.         var div=document.getElementById("div2");  
  56.         if(div != null){  
  57.             div.style.backgroundColor="#FFFFFF";  
  58.         }  
  59.     }  
  60.     function outDIV2(){  
  61.         var div=document.getElementById("div2");  
  62.         if(div != null){  
  63.             div.style.backgroundColor="yellow";  
  64.         }  
  65.     }  
  66. </script>  
  67. </head>  
  68. <body>  
  69. <br>  
  70. <br>  
  71. <div align="center"><input type="button" value="打开窗口"  
  72.     onclick="openWin()"></div>  
  73. <div id="div1">  
  74. <div id="div2" onclick="closeWin()" onmouseover="overDIV2()"  
  75.     onmouseout="outDIV2()">DEL</div>  
  76. <div id="div3">这是我们能看见的窗口,点一下DEL就消失了!</div>  
  77. </div>  
  78. </body>  
  79. </html>  

原来也是很简单的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值