Home >Web Front-end >JS Tutorial >JavaScript imitation Weibo publishing information case

JavaScript imitation Weibo publishing information case

高洛峰
高洛峰Original
2016-12-06 16:11:361359browse

Nowadays, there are many effects similar to posting updates on Weibo. The following is a small case written in JavaScript that is similar to posting information on Weibo

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>微博发布</title>
  <style type="text/css">
 
    *{
      padding: 0;
      margin: 0;
    }
    ul {
 
      list-style: none;
    }
 
    .box {
      width: 600px;
      height: auto;
      border: 1px solid #ccc;
      margin: 100px auto;
      text-align: center;
      padding: 30px 0;
      background-color: rosybrown;
    }
 
    .box textarea {
      width: 450px;
      resize: none; /*设置文本不能过拖动*/
    }
    .box li {
 
      width: 450px;
      line-height: 30px;
      border-bottom: 1px dashed #ccc;
      margin-left: 80px;
      text-align: left;
    }
 
    .box li a {
 
      float: right;
    }
  </style>
 
  <script type="text/javascript">
 
    window.onload = function (){
      //获取数组的第一个
      var btn = document.getElementsByTagName("button")[0];
      var txt = document.getElementsByTagName("textarea")[0];
      var ul = document.createElement("ul");
      btn.parentNode.appendChild(ul); //添加子节点
      btn.onclick = function (){
 
        //1.需要判断文本中是否有内容
        if(txt.value == ""){
 
          alert("亲!内容不能为空哦!!");
 
          return false; //让操作就在这个地方终止
        }
 
        var newli = document.createElement("li"); //创建一个新的li标签
        newli.innerHTML = txt.value +"<a href=&#39;javascript:;&#39;>删除</a>"; //给新的标签添加内容,并拼接删除已连接
        ul.appendChild(newli);
 
        //清空输入框
        txt.value = "";
 
        var aa = document.getElementsByTagName("a");//获取所有的a标签
 
        for(var i = 0;i<aa.length;i++){ // 遍历点击删除
 
          aa[i].onclick = function () {
 
            this.parentNode.remove();
          }
        }
 
 
      }
 
    }
 
  </script>
</head>
<body>
<div class="box">
 
  微博发布:<textarea name="" id="" cols="30" rows="10"></textarea>
  <button>发布</button>
 
</div>
</body>
</html>

The style laid out, click the blue delete link, it will be deleted The corresponding line of content.

JavaScript imitation Weibo publishing information case

Momo said: Important knowledge points: get the value of the input box, create sub-nodes and add content to the sub-nodes, and delete the corresponding nodes.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn