微博发布案例

<!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: #666699;
        }

        .box textarea {
            width: 450px;
            resize: none; /*设置文本不能过拖动*/
        }

        .box li {
            width: 500px;
            line-height: 30px;
            border-bottom: 1px dashed #ccc;
            margin-left: 80px;
            text-align: left;
        }

        .box li a {
            float: right;
        }

        button {
            width: 60px;
            height: 30px;
            background-color: #ccc;
            border: 1px solid #ccc;
            outline: none;
        }
    </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");
                newli.innerHTML = txt.value + "<a href='javascript:;'>删除</a>";
                ul.appendChild(newli);
                txt.value = "";
                var aa = document.getElementsByTagName("a");
                for (var i = 0; i < aa.length; i++) { // 遍历点击删除
                    aa[i].onclick = function () {
                        this.parentNode.remove();
                    }
                }
            }
        }

    </script>
</head>
<body>
<div class="box">
    <p>微博发布</p>
    <textarea name="" id="" cols="30" rows="10"></textarea></br>
    <button>发布</button>
</div>
</body>
</html>

 

posted @ 2019-01-22 18:51  yang_xiaoxi  阅读(209)  评论(0编辑  收藏  举报