js實現微博發布小功能,js實現微博

來源:互聯網
上載者:User

js實現微博發布小功能,js實現微博

微博發布功能,可發布可刪除。樣式沒設定忽略,主要知識點及注意點:

1、動態添加節點標籤。

2、內容為空白時不能發布。

3、新發布的內容要在上面。

4、內容刪除要同時刪除掉節點。

5、為保持樣式輸入框要設定為不可拖拽。

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style> * {margin: 0;padding: 0;} .box{ border: 1px solid #000; width: 600px; height: auto; margin:100px auto; padding:30px 0; } textarea{ width: 450px; resize:none; margin-left: 20px; } ul li{ width: 450px; list-style: none; border-bottom: 1px dotted #ccc; margin-left:20px; line-height: 30px; color: #333; } ul li a{ float: right; font-size: 12px; } </style> <script src="../jquery-1.11.1.min.js"></script> <script type="text/javascript">  // 方法1 window.onload = function () { var box = document.getElementById("box"); var boys=box.children; var ul = document.createElement("ul"); box.appendChild(ul); boys[2].onclick = function (){ if(boys[1].value==""){  alert("輸入不可為空");  return; } var newli= document.createElement("li"); newli.innerHTML=boys[1].value+"<a href='javascript:;'>刪除</a>"; boys[1].value =""; var lis=ul.children; if(lis.length==0){ul.appendChild(newli);}else{  ul.insertBefore(newli, lis[0]); } var as=document.getElementsByTagName("a"); for (var i=0;i<as.length;i++){  as[i].onclick=function(){  ul.removeChild(this.parentNode);  } } } } // 方法2 引用jQuery // $(function(){ // $("<ul></ul>").appendTo("#box"); // var $text = $("#box").find("textarea"); // var $btn = $("#box").find("button"); // $btn.on("click",function(){ // if($text.val() ==""){ // alert("請輸入內容"); // return; // } // $("ul").prepend("<li>"+$text.val()+"<a href='javascript:;'>刪除</a></li>"); // $text.val(""); // $("a").on("click",function(){ // $(this).parent("li").remove(); // }) // }) // }) </script></head><body> <div class="box" id="box"> 微博發布:</br><textarea cols="30" rows="10"></textarea> <button>發布</button> </div></body></html>

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的協助,同時也希望多多支援幫客之家!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.