2019-05-21day6作业

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>动态删除和添加</title>
        <style type="text/css">
            #box{
                width: 200px;
                height: auto;
                position: absolute;
                margin: auto;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
            }
            .box1{
                width: 200px;
                height: 45px;
                font-size: 25px;
                background-color: red;
                text-align: center;
                margin-bottom: 5px;
            }
            .f1{
                color: white;
                line-height: 45px;
            }
            .f2{
                float: right;
                line-height: 45px;
                color: white;
                font-size: 20px;
                background-color: rgba(0,0,0,0);
                border: none;
                
            }
            #input1{
                width: 148px;
                height: 30px;
                border-top: 0;
                border-right: 0;
                border-left: 0;
                border-bottom-color: black;
                /*background-color: rgb();*/
                
            }
            #input1:focus{
                outline: 0;
            }
            #dl{
                height: 30px;
                background-color: red;
                border: none;
            }
            #dl:focus{
                outline: 0;
            }
        </style>
        
    </head>
    <body>

        <div id="box">
            <div class="box1">
                <font class="f1">桃子</font>
                <button class="f2">x</button>
            </div>
            <div class="box1">
                <font class="f1">李子</font>
                <button class="f2">x</button>
            </div>
            <div class="box1">
                <font class="f1">李子</font>
                <button class="f2">x</button>
            </div>
            <input type="text" name="input1" id="input1" value=""/>
            <input type="button" id="dl" value="确认" />
        </div>
    </body>
</html>

<script type="text/javascript">
    boxNode = document.getElementById('box')
    inputNode = document.getElementById('input1')
    dlNode = document.getElementById('dl')
    Nodes = document.getElementsByClassName('f2')
    //添加
    dlNode.onclick = function(){
        if(inputNode.value!=false){
            divNode = document.createElement('div')
            divNode.class='box1'
            divNode.style='width: 200px;height: 45px;font-size: 25px;text-align: center;margin-bottom: 5px;'
            red = parseInt(Math.random()*255)
            green = parseInt(Math.random()*255)
            blue = parseInt(Math.random()*255)
            divNode.style.backgroundColor = 'rgba('+red+','+green+','+blue+')'
            f1Node = document.createElement('font')
            f1Node.innerText = inputNode.value
            f2Node = document.createElement('button')
            f2Node.className= 'f2'
            f2Node.innerText = 'x'
            f1Node.style='color: white;line-height: 45px;'
            f2Node.style='float: right;line-height: 45px;color: white;font-size: 20px;background-color: rgba(0,0,0,0);border: none;'
            divNode.appendChild(f1Node)
            divNode.appendChild(f2Node)
            boxNode.insertBefore(divNode,inputNode)
            Nodes = document.getElementsByClassName('f2')
            
            console.log(Nodes.length)
            inputNode.value=''
            func1()
        }else{
            alert('请在输入框中输入内容')
        }
    }

    console.log(Nodes)
    function func1(){
        btn1Nodes = document.getElementsByClassName('f1')
        for (x=0;x<Nodes.length;x++) {
            console.log(Nodes)
            Nodes[x].onclick =function(){
                result= confirm('是否删除?')
                if (result) {
                    this.parentElement.remove()
                    func1()
                } 
            }       
        }

    }
    btn1Nodes = document.getElementsByClassName('f1')
    for (x=0;x<Nodes.length;x++) {
        console.log(Nodes)
        Nodes[x].onclick =function(){
            result= confirm('是否删除?')
            if (result) {
                this.parentElement.remove()
            } 
        }       
    }
    
    
    
</script>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容