欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript實(shí)現(xiàn)留言板功能

 更新時間:2020年02月08日 07:39:49   作者:空谷丶幽蘭  
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)留言板功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了javascript實(shí)現(xiàn)留言板功能的具體代碼,供大家參考,具體內(nèi)容如下

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <style>
 *{ 
    margin: 0;
    padding: 0;
 }
 .box{ /*設(shè)置最外層盒子*/
 width: 600px;
 border: 1px solid #aaa;
 padding: 20px 10px;
 margin: 100px auto;
 }
 #plTxt{ /*設(shè)置文本域*/
 width: 450;
 resize: none;/*防止用戶拖拽*/
 }
 .box ul{ /*將ul列表去除前面的點(diǎn)*/
 list-style: none;
 }
 .box ul li{ /*設(shè)置li中的評論文字樣式*/
 width: 450px;
 line-height: 30px;
 border-bottom: 1px dotted #aaa;
 margin-left: 50px;
 }
 .box ul li a{ /*將刪除的樣式更改顏色,向右浮動,沒有下劃線*/
 color: orange;
 float: right; 
 text-decoration: none;
 
 }
 .box ul li .time{ /*將li中的時間改為向右浮動和改顏色*/
 color: #4f0;
 float: right;
 }
 </style>
 <script>
  window.onload = function(){
  function $(id){
  return document.getElementById(id);
  }
  var ul=document.createElement('ul'); //創(chuàng)建ul標(biāo)簽
  $('pl').appendChild(ul); //把ul標(biāo)簽放在div里面
  $('btn').onclick = function (){
     var txt = $('plTxt').value; //此時不能用$('plTxt').innerHTML,成對的標(biāo)簽使用innerHTNL,獲得里面文字;
     if(txt.length==0){  //判斷輸入為空的情況;
     alert('不能發(fā)表為空的評論');
     }else{
     var li=document.createElement('li'); //創(chuàng)建li標(biāo)簽
     ul.appendChild(li);  // li添加為ul的子標(biāo)簽
     txt = txt+ "<a href='javascript:void(0)'>刪除</a>" + "<span class='time'>" + new Date().toLocaleTimeString() + "</span>";
      li.innerHTML = txt; //將文本賦給li標(biāo)簽中顯示
     var dels =document.getElementsByTagName('a'); //獲取所有標(biāo)簽a的id存到數(shù)組中
     for(var j=0; j<dels.length; j++){
     dels[j].onclick=function(){ //將所有a標(biāo)簽設(shè)置點(diǎn)擊事件
             //刪除當(dāng)前評論,就是刪除當(dāng)前“刪除”所在超鏈接的li
      ul.removeChild(this.parentNode);
     }
     }
     }
    }
  }
 </script>
</head>
<body>
 <div class="box" id="pl">
 <span>發(fā)表評論:</span>
 <textarea id="plTxt" cols="30" rows="10"></textarea>
 <input type="button" value="評論" id="btn" >
 </div>
 
</body>
</html>

新增加的文字放在后面,將代碼中的

ul.appendChild(li); //(li添加為ul的子標(biāo)簽,每次都放在末尾)換成:
ul.insertBefore(li,ul.children[0]); //——這行代碼為插入,將當(dāng)前的li標(biāo)簽,插在ul的第一個子標(biāo)簽之前,即新的li標(biāo)簽每次都插入在最前面。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論