實(shí)操

image.png

1. 制作動(dòng)態(tài)刪除和添加內(nèi)容

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>制作動(dòng)態(tài)刪除和添加內(nèi)容</title>
        <style type="text/css">
            .div{
                position:relative;
                background-color: darkseagreen;
                text-align: center;
                width:200px;
                height: 40px;
                
                margin-top: 1px;
                
            }
            span{
                position: absolute;
                height: 40px;
                /*line-height: 40px;*/
                top: 14px;
                left: 80px;
                vertical-align: middle;
            }
            font{
                /*text-align: center;*/
                position: absolute;
                right: 3px;
                top: 12px;
            }
            input{
                height: 40px;
                border:0;
                border-bottom:1px solid rgb(150,150,150)
            }
            input:focus{
                outline:none;
            }
            button{
                background-color: sandybrown;
                color: white;
                height: 40px;
                width: 50px;
            }
        </style>
    </head>
    <body>
        <div class="div" id="div1">
            <span id="">
                西瓜
            </span>
            <font>X</font>
        </div>
        <div class="div">
            <span id="">
                火龍果
            </span>
            <font>X</font>
        </div>
        <div class="div">
            <span id="">
                香蕉
            </span>
            <font>X</font>
        </div>
        <div class="div">
            <span id="">
                蘋果
            </span>
            <font>X</font>
        </div>
        <div>
            <input type="text" name="name" id="name" value="" /><label for="name"><button id="add">確定</button></label>
            
        </div>
        <script>
            fontNodes = document.getElementsByTagName("font")
            console.log(fontNodes)
            for (x in fontNodes){
                fontNodes[x].onclick = function (){
                    this.parentElement.remove()
                }
            }
            function addItem(value){
                addDivNode = document.createElement("div")
                addDivNode.style = "class:div;position: relative;float:left;text-align: center;width:200px;height: 40px;"
                console.log(addDivNode)
                addSpanNode = document.createElement("span")
                addSpanNode.innerText = value
                addSpanNode.style = "position: absolute;float:left;top: 14px;left: 80px;vertical-align: middle;"
                addFontNode = document.getElementsByTagName("font")[0]
                addFontNode.innerText = "X"
                addFontNode.style = "position: absolute;right: 3px;top: 12px;"
                addFontNode.onclick = function (){
                    this.parentElement.remove()
                }
                addDivNode.appendChild(addFontNode)
                redNum = Math.random()*255+1
                greenNum = Math.random()*255+1
                blueNum = Math.random()*255+1
                
                addDivNode.appendChild(addSpanNode)
                addDivNode.style = "background-color: rgb("+redNum+","+greenNum+","+blueNum+");class:div;position: relative;margin-top: 1px;text-align: center;width:200px;height: 40px;"
                bodyNode = document.getElementsByTagName("body")[0]
                console.log(bodyNode)
                bodyNode.insertBefore(addDivNode,bodyNode.firstElementChild)
                console.log(bodyNode)
            }
            buttonNode = document.getElementById("add")
            console.log(buttonNode)
            inputNode = document.getElementsByTagName("input")[0]
            
            inputNode.onchange = function(){
                value = inputNode.value
                console.log(inputNode.value)
                buttonNode.onclick = addItem(value)
            }
            
        </script>
    </body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容