QQ聊天消息顯示,提交評論等實現原理,具體內容如下
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> * { margin: 0; padding: 0; } .bos { margin: 100px auto; width: 350px; position: relative; } .bos a { float: right; } button { position: relative; left: 301px; bottom: 0; } textarea { width: 350px; resize: none; } ul li { list-style: none; } </style> <script type="text/javascript"> window.onload = function() { var txt = document.getElementById('txt'); var btn = document.getElementsByTagName('button')[0]; var oUl = document.getElementsByTagName('ul')[0]; btn.onclick = function() { if(txt.value == '') { alert('請輸入...'); return false; //結束事件******* } var newli = document.createElement('li'); //創建標簽<li></li> newli.innerHTML = txt.value + '<a href = "#">刪除<a>'; //oUl.appendChild(newli); //將創建標簽<li></li>加到最后面 var lis = oUl.childNodes; //oUl.children oUl.insertBefore(newli, lis[0]); //將創建標簽<li></li>加到最前面 txt.value = ''; //刪除發出去的消息 var oA = document.getElementsByTagName('a'); for(var i = 0; i < oA.length; i++) { oA[i].onclick = function() { oUl.removeChild(this.parentNode); } } } } </script> </head> <body> <div id="box" class="bos"> <textarea name="" id="txt" cols="30" rows="10"></textarea> <button>submit</button> <ul></ul> </div> </body></html>以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答