许愿树——JS练习

一个简单的许愿树制作,记录一下


<!DOCTYPE html>
<html>
<head>
   <title>许愿树</title>
   <meta http-equiv="content-type" content="text/html;charset=utf-8">
   <style type="text/css">
      *{  margin: 0;  padding: 0;  }
      body{  background-image:url(images/bg.jpg);  font-weight: bold; }
      .box{  width: 400px;  height: 500px;  }
      .pink,.blue,.purple{  width:235px;  height: auto;  }
      .top{  width: 235px;  height: 68px;  }
      .middle{  width: 217px;  min-height: 50px;  margin-left: -2px;  padding-left: 20px;  color: #000;  }
      .bottom{  width: 235px;  height: 72px;  color: #000;  }
      .top.blue{       background-image: url(images/a6_1.gif);    }
      .middle.blue{  background-image: url(images/a6_2.gif);  }
      .bottom.blue{  background-image: url(images/a6_3.gif);  }
       .top.purple{  background-image: url(images/a4_1.gif);  }
      .middle.purple{  background-image: url(images/a4_2.gif);  }
      .bottom.purple{  background-image: url(images/a4_3.gif);  }
       .top.pink{  background-image: url(images/a3_1.gif);  }
      .middle.pink{  background-image: url(images/a3_2.gif);  }
      .bottom.pink{  background-image: url(images/a3_3.gif);  }
      .bottom .wishIcon{  padding: 20px 0 0 30px; float: left;  }
      .bottom .wishName{  padding:20px 5px 0 30px;float: right;  }
   </style>
</head>
<body>
   <div class="box">
      <label for="name" >许愿留名:</label><input type="text" id="name"><br><br>
      <label for="content">许愿内容:</label> <textarea rows="10" cols="20"  id="content"></textarea><br>
      <label for="icon" id="icon">许愿图标:</label>
      <input type="radio" name="apple" value="0" /><label for="apple" name="a"><img src="images/bpic_11.gif"></label>
      <input type="radio" name="apple" value="1" /><label for="heart" name="a"><img src="images/bpic_13.gif"></label>
      <input type="radio" name="apple" value="2" /><label for="leaf" name="a"><img src="images/bpic_25.gif"></label>
      <br><br>
      <label for="content">许愿颜色:</label>
      <select id="color">
         <option value="blue">淡蓝色</option>
         <option value="purple">黄色</option>
         <option value="pink">粉色</option>
      </select>
      <br>
      <input type="button" value="许愿" οnclick="xuyuan()">
      <br>
   </div>
</body>
<script type="text/javascript">
   var num=0;
   function xuyuan() {
      num++;
      var wishName=document.getElementById("name").value;//获取许愿姓名的值
      var wishContent=document.getElementById("content").value;//获取许愿内容的值
      var wishColor=document.getElementById("color").value;//获取许愿颜色
      var  myselect=document.getElementsByName("apple");//获取许愿图标前边的值
      var labelArr=document.getElementsByName("a");//获取许愿图标
      var bg="";
      for (var i = 0; i < myselect.length; i++) {
         if (myselect[i].checked) {
            bg=labelArr[i].innerHTML;
         }
      }
      addWisher(wishName,wishContent,wishColor,bg,num);
   }
   function addWisher(wishName,Content,color,bg,i){
      var div=document.createElement("div");
      document.getElementsByTagName("body")[0].appendChild(div);
      div.style.position="absolute";
      div.style.left=Math.random()*800+300+"px";
      div.style.top=Math.random()*400+"px";
      div.style.zIndex=i;
      div.innerHTML="<div class='top "+color+"'></div>";
      div.innerHTML+="<div class='middle "+color+"'>"+Content+"</div>";
      div.innerHTML+="<div class='bottom "+color+"'><span class='wishIcon'>"+bg+"</span><span class='wishName'>"+wishName+"</span></div>";

   }
</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值