Javascript节点操作的案例(原码)

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<style>

    .box{margin-left: 500px;}

    #div1{width: 230px; height: 80px; background-color: lavender; border: 1px solid gray;}

    #div1 input{width: 210px; margin: 5px;  font-size: 18px;}

    #div1 button{margin:3px 8px; font-size: 14px;}

    #div2 {width: 230px; height: 300px; background-color: rgb(246, 247, 248); border: 1px solid gray; border-bottom: gray dashed 1px;}

    #div2 div{border-bottom: 1px dashed gray; position: relative;}

    #div2 button{position: absolute; right: 0;}

</style>

<script>

   window.οnlοad=function(){

       var odiv1=document.getElementById('div1');

       var odiv2=document.getElementById('div2');

       var oinput=document.getElementById('input1');

       var aBtns=odiv1.getElementsByTagName('button');

       aBtns[0].οnclick=function(){

           if(!oinput.value){

               alert("输入的内容不能为空");

           }else{

               var newdiv=document.createElement("div")

               var text=document.createTextNode(oinput.value);

               newdiv.appendChild(text);

               newdiv.style.backgroundColor=random();

               odiv2.appendChild(newdiv);

               oinput.value='';

           }

           

       }

       aBtns[1].οnclick=function(){

        odiv2.removeChild(odiv2.lastChild);

       }

       aBtns[2].οnclick=function(){

        var newnode=odiv2.lastChild.cloneNode(true);

        odiv2.appendChild(newnode);

       }

    }

   function random(){

        var str="rgba("+parseInt(Math.random()*256)+','+parseInt( Math.random()*256)+","+parseInt( Math.random()*256)+",1)";

        return str;

    }

</script>

<body>

    <div class="box">

    <div id="div1">

        <input type="text" id="input1" placeholder="请输入内容:"/><br/>

        <button>增加</button>

        <button>删除</button>

        <button>拷贝</button>

    </div>

    <div id="div2">

    </div>

</div>

</body>

</html>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值