javascript - 网站上看到一个这样点击按钮控制DIV的变化的效果,试着写了下,效果可以实现,这样写有什么不好的地方吗?或者怎么写可以提高性能
迷茫
迷茫 2017-04-11 09:40:26
[JavaScript讨论组]
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body style="height:4000px">

<style>
    • {
      margin: 0;
      padding: 0;
      }

      #btn {
      text-align: center;
      margin-top: 10px;
      }

      #main {
      background-color: #008800;
      width: 100px;
      height: 100px;
      margin: 30px auto;
      }
      </style>

      <p id="btn">

       <button id="wider">变宽</button>
       <button id="higher">变高</button>
       <button id="change-color">变色</button>
       <button id="hide">隐藏</button>
       <button id="reset">重置</button>

      </p>

    <p id="wrap">

     <p id="main"></p>

    </p>

    <script>

    1. i=0;

    2. op = document.getElementById('main');

    3. oBtn = document.getElementsByTagName('button');
      for(i,len=oBtn.length;i<len;i++){

       oBtn[i].onclick=function(){
      
           switch(this.id){
      
             case "wider" :
               op.style.width = "200px";
               break;
             case "higher" : 
               op.style.height = "200px";
               break;
             case "change-color" : 
               op.style.background = "red";
               break;
             case "hide" : 
               op.style.display = "none";
               break;
             case "reset" : 
               op.style.background = "#008800";
               op.style.width = "100px";
               op.style.height = "100px";
               op.style.display = "block";
           }
      
       }

      }
      </script>

      </body>
      </html>

    迷茫
    迷茫

    业精于勤,荒于嬉;行成于思,毁于随。

    全部回复(0)
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号