1. 程式人生 > >簡單模擬評論效果

簡單模擬評論效果

click element gin 分享圖片 black info 函數 parse border

簡單模擬評論效果

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>模擬評論</title>
        <style>
            #div1{width: 400px; height: 200px; background-color: gray; text-align: center; margin: 0 auto;}
            #div2{width: 400px; height: 600px; background
-color: orange; margin: 0 auto;} #div2 div{width: 400px; height: 20px; text-align: center; line-height: 20px; font-size: 16px; border-bottom: 1px dashed black; margin-top: 2px} #input1{width: 300px; height: 30px; margin-top: 50px; font-size: 18px} #div1 button{width: 100px; height: 30px; font
-size: 18px} </style> <script> window.onload = function(){ //獲取元素節點 var oDiv1 = document.getElementById(‘div1‘); var oDiv2 = document.getElementById(‘div2‘); var oInput = document.getElementById(‘input1‘);
//獲取三個按鈕 var aBtns = oDiv1.getElementsByTagName(‘button‘); //增加 aBtns[0].onclick = function(){ var node = document.createElement(‘div‘); node.innerHTML = oInput.value;//節點內容等於input輸入框裏的內容 node.style.backgroundColor = randomColor();//添加一個隨機顏色 oDiv2.appendChild(node);//把節點node插入到oDiv2裏最後 } //刪除 aBtns[1].onclick = function(){ oDiv2.removeChild(oDiv2.lastElementChild);//在oDiv2元素節點裏移除最後一個oDiv節點裏的最後一個子節點 } //拷貝 aBtns[2].onclick = function(){ //拷貝最後一條記錄 var node = oDiv2.lastElementChild.cloneNode(true);//傳入參數 true 說明完全克隆拷貝 oDiv2.appendChild(node);//把節點node插入到oDiv2裏最後 } } /*-------------封裝隨機顏色函數--------*/ function randomColor(){ var str = "rgba(" + parseInt(Math.random() * 256) + "," + parseInt(Math.random() * 256) + "," + parseInt(Math.random() * 256) + ",1)"; return str; } /*-------------封裝隨機顏色函數end--------*/ </script> </head> <body> <div id = ‘div1‘> <input type="text" placeholder="請輸入文本" id = ‘input1‘ /><br/> <button>增加</button> <button>刪除</button> <button>拷貝</button> </div> <div id = ‘div2‘> <!-- <div>ssss</div> <div>ssdadad</div> --> </div> </body> </html>

瀏覽器效果:

技術分享圖片

簡單模擬評論效果