妙味课堂作业20160113(优化版)

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>2016011220160115</title>

    <style>
      #set_color{
        display: inline-block;
        width: 80px;
        text-align: center;
        background-color: red;
        font-size: 16px;
        padding: 10px 0;
        color:white; 
        cursor: pointer;  
      }
      #layer{
        display: none;
      }

      #layer_bg{
        background-color: black;
        opacity: 0.2;
        top:0;
        left:0;
        width: 100%;
        height:100%;
        position: fixed; 
        z-index: 10;
      }
      #layer_content{
        width: 15%;
        margin-left: 40%;
        background-color: white;
        opacity: 1;
        padding: 20px;
        z-index: 20;
        position: absolute; 
      }
      #target_div{
        border: 5px solid #000;
        width: 100px;
        height:100px;
      }
    </style>
  </head>

  <body>
    <p><strong>请为下面的DIV设置样式:</strong><span id="set_color">点击设置</span></p>
    <div id="target_div">
    </div>
    <div id="layer">
      <div id="layer_bg">
      </div>
      <div id="layer_content">
        <p>
          <span>请选择背景色:</span>
            <button value="红">红</button>
            <button value="黄">黄</button>
            <button value="蓝">蓝</button>
        </p>
        <p>
          请选择宽(PX):
            <button value="200">200</button>
            <button value="300">300</button>
            <button value="400">400</button>
        </p>
        <p>
          请选择高(PX):
            <button value="200">200</button>
            <button value="300">300</button>
            <button value="400">400</button>
        </p>
          <button value="恢复">恢复</button>
          <button value="确定">确定</button>
      </div>
    </div>


    <script type="text/javascript">
    var btns = document.getElementsByTagName('button');
    var tarDiv = document.getElementById('target_div');
    var colors = ['red','yellow','blue','200px','300px','400px'];
    for (var i = 0; i < btns.length - 1; i++) {
        btns[i].onclick = function(i){
      /********闭包的样例,破解闭包***********/
          return function(){
          if(i>=0 && i<=2){
            tarDiv.style.background = colors[i];
          }
          else if(i>=3 && i<=5){
            tarDiv.style.width = colors[i];
          }
          else if(i>=6 && i<=8){
            tarDiv.style.height = colors[i-3];
          }
          };
        }(i);
    };

    btns[9].onclick = function(){
      tarDiv.style.background = 'white';
      tarDiv.style.width = '100px';
      tarDiv.style.height = '100px';
    }

      btns[3].onmouseover = function(){
      tarDiv.style.background = '#CC6666';
      tarDiv.style.width = '200px';
    }
    btns[10].onclick = function(){
      document.getElementById('layer').style.display = 'none';
    }
    document.getElementById('set_color').onclick = function(){
      document.getElementById('layer').style.display = 'block';
    };

    </script>
  </body>
</html>

 

转载于:https://www.cnblogs.com/xiayedexin/p/5157253.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值