JS属性操作实例

来源:互联网 发布:同花顺炒股软件好吗 编辑:程序博客网 时间:2024/05/21 10:08


js
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <!doctype html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <title>js属性操作实例</title>  
  6. <style>  
  7. *{ padding:0; margin:0;}  
  8. ul{ list-style:none;}  
  9. a{ text-decoration: none; color:#333;}  
  10. .clearfix{zoom:1;}  
  11. .clearfix:after{ clear:both; content:""; height:0; display:block; overflow:hidden;}  
  12. .wrap{ padding:10px; font-size:12px; font-family:"Microsoft Yahei";}  
  13. .show-tt h2{ float:left; margin-right:10px;}  
  14. .set-btn{ background:#39F; color:#fff; padding:5px 10px; border:1px solid #38F;}  
  15. .box1{ width:100px; height:100px; background:#ccc; border:1px solid #aaa;}  
  16. .box2{ width:300px; height:200px; background:#fff; border:15px solid #999; position:absolute; z-index:1; left:40%; top:35%; display:none;}  
  17. .box2-tt{ font-size:14px; font-weight:bold; font-family:"微软雅黑"; color:#000; list-style:none; line-height:40px; padding:10px 0 0 10px; width:115px; float:left; border:0px solid red;}  
  18. .box2-nr{ width:171px; float:left; border:0px solid green;}  
  19. .box2-nr a{ display:block; width:40px; text-align:center; margin-left:10px; margin-top:10px; background:#eee; border:1px solid #ddd; float:left; height:32px; line-height:32px;}  
  20. .box2-nr .bg-red{ background:red; color:white; font-weight:bold;}  
  21. .box2-nr .bg-yellow{ background:yellow; font-weight:bold;}  
  22. .box2-nr .bg-blue{ background:blue; color:white; font-weight:bold;}  
  23. .btn-group{ text-align:center; margin-top:10px;}  
  24. .submit ,.reset{ background:#eee; border:1px solid #aaa; padding:5px 10px; margin:0 5px; cursor:pointer;}  
  25. .mask{ width:100%; height:100%; background: black; position:absolute; top:0px; left:0; right:0; bottom:0; filter:alpha(opacity=20); opacity: 0.2; display:none; }  
  26. </style>  
  27. <script>  
  28. window.onload = function(){  
  29.     var oSetbtn = document.getElementById('set-btn');     
  30.     var oBox2btn = document.getElementById('box2-btn');  
  31.     var aBox2btns = oBox2btn.getElementsByTagName('a');  
  32.     var oBtnGroup = document.getElementById('btn-group');  
  33.     var aInput = oBtnGroup.getElementsByTagName('input');  
  34.     var oBox1 = document.getElementById('box1');  
  35.     var oBox2 = document.getElementById('box2');  
  36.     var oMask = document.getElementById('mask');  
  37.       
  38.     oSetbtn.onclick = function(){  
  39.         oBox2.style.display = 'block';  
  40.         oMask.style.display = 'block';    
  41.     };  
  42.       
  43.     //变红  
  44.     aBox2btns[0].onclick = function(){  
  45.         oBox1.style.background = 'red';   
  46.     };  
  47.       
  48.     //变黄  
  49.     aBox2btns[1].onclick = function(){  
  50.         oBox1.style.background = 'yellow';    
  51.     };  
  52.       
  53.     //变蓝  
  54.     aBox2btns[2].onclick = function(){  
  55.         oBox1.style.background = 'blue';      
  56.     };  
  57.       
  58.     //变宽到200  
  59.     aBox2btns[3].onclick = function(){  
  60.         oBox1.style.width = '200px';      
  61.     };  
  62.       
  63.     //变宽到300  
  64.     aBox2btns[4].onclick = function(){  
  65.         oBox1.style.width = '300px';      
  66.     };  
  67.       
  68.     //变宽到400  
  69.     aBox2btns[5].onclick = function(){  
  70.         oBox1.style.width = '400px';      
  71.     };  
  72.       
  73.     //变高到200  
  74.     aBox2btns[6].onclick = function(){  
  75.         oBox1.style.height = '200px';     
  76.     };  
  77.       
  78.     //变高到300  
  79.     aBox2btns[7].onclick = function(){  
  80.         oBox1.style.height = '300px';     
  81.     };  
  82.       
  83.     //变高到400  
  84.     aBox2btns[8].onclick = function(){  
  85.         oBox1.style.height = '400px';     
  86.     };  
  87.       
  88.     //确定  
  89.     aInput[0].onclick = function(){  
  90.         oBox2.style.display = 'none';  
  91.         oMask.style.display = 'none';         
  92.     };  
  93.       
  94.     //重置  
  95.     aInput[1].onclick = function(){  
  96.         oBox1.style.background = '#ccc';  
  97.         oBox1.style.width = '100px';  
  98.         oBox1.style.height = '100px';     
  99.     };  
  100. };  
  101. </script>  
  102. </head>  
  103.   
  104. <body>  
  105. <div class="wrap">  
  106.     <div class="show-tt clearfix">  
  107.         <h2>请为下面的DIV设置样式</h2>  
  108.         <a id="set-btn" class="set-btn" href="javascript:;">点击设置</a>  
  109.     </div>  
  110.     <div id="box1" class="box1"></div>  
  111.       
  112.     <div id="box2" class="box2">  
  113.         <div class="box2-cont clearfix">  
  114.             <div class="box2-tt">  
  115.                 <ul>  
  116.                     <li>请选择背景色:</li>  
  117.                     <li>请选择宽度(PX):</li>  
  118.                     <li>请选择高度(PX):</li>  
  119.                 </ul>  
  120.             </div>  
  121.             <div id="box2-btn" class="box2-nr">  
  122.                 <a class="bg-red" href="javascript:;"></a>  
  123.                 <a class="bg-yellow" href="javascript:;"></a>  
  124.                 <a class="bg-blue" href="javascript:;"></a>  
  125.                 <a href="javascript:;">200</a>  
  126.                 <a href="javascript:;">300</a>  
  127.                 <a href="javascript:;">400</a>  
  128.                 <a href="javascript:;">200</a>  
  129.                 <a href="javascript:;">300</a>  
  130.                 <a href="javascript:;">400</a>  
  131.             </div>  
  132.         </div>  
  133.           
  134.         <div id="btn-group" class="btn-group">  
  135.             <input class="submit" type="submit" value="确定" />  
  136.             <input class="reset" type="reset" value="重置" />  
  137.         </div>  
  138.     </div>  
  139.     <div id="mask" class="mask"></div>  
  140. </div>  
  141. </body>  
  142. </html> 
0 0
原创粉丝点击