js-微博发布-练手

微博发布评论,评论区动态增加刚发布的内容,并提供删除功能。练习创建节点、追加节点、移除节点。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微博发布</title>
    <style>
        ul{
            margin: 0;
            padding: 0;
            list-style: none;
        }
        .box{
            width: 500px;
            height: auto;
            margin: 100px auto;
            border: 1px solid #eee;
            padding: 10px 0;
        }
        .box textarea{
            width: 300px;
            height: 200px;
            resize:none;
        }
        .commands{
            margin-left: 90px;
        }
        .commands li{
            width: 300px;
            height: 30px;
            line-height: 30px;
            border-bottom: 1px dashed #666;
        }
        .commands li a{
            float: right;
        }
    </style>
    <script>    
    window.onload = function(){
        function $(id){ return document.getElementById(id); }
        $("btn").onclick = function(){
            if($('txt').value==""){
                alert("输入内容不能为空");
                return;
            }else{
                var newli = document.createElement("li");
                newli.innerHTML = $('txt').value + "<a href='javascript:;'>删除</a>";
                var childrens =$('ul').children;
                if(childrens.length==0){
                    $('ul').appendChild(newli);
                }else{
                    $('ul').insertBefore(newli,childrens[0]);
                }
                $('txt').value = "";
            }
            var as = $("ul").getElementsByTagName("a");
            for(var j=0; j<as.length; j++){
                as[j].onclick = function(){
                    $('ul').removeChild(this.parentNode);
                }
            }
        }
    }
    </script>
</head>
<body>
    <div class="box">
        <div class="write">
            微博发布:
            <textarea name="" id="txt" cols="30" rows="10"></textarea>
            <button id="btn">发布</button>
        </div>
        <ul id="ul" class="commands">
            <!-- <li>123</li>
            <li>asdf</li>
            <li>wqe2</li> -->
        </ul>
    </div>
</body>
</html> 

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值