jQuery實現微博發佈界面

知識點

  1. resize: none :textarea不允許拉動
  2. text-indent :規定文本塊中首行文本的縮進
  3. $.trim(str):去掉字符串起始和結尾的空格
  4. focus([[data],fn]):當元素獲得焦點時,觸發 focus 事件
  5. 如果只將點擊事件綁定在li標籤上,新增的li標籤不會帶有綁定事件。
    解決方案:
    ① 在新增li標籤時候,爲其增加綁定事件。但代碼較爲冗餘。
    ② 事件委託。將事件綁定在父級標籤上。
    語法:
    父級標籤.on(事件,事件源,觸發函數)
    
    例如:
    $('.dataList>ul').on('click', 'li', function () {
        $(this).animate({
            'width': '0px'
        }, 1000, function () {
            $(this).remove();
        });
    });
    

代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {padding: 0;margin: 0;list-style: none;}
        body {background-image: url("images/body_bg.jpg");}
        .box {width: 700px;margin:100px auto;}
        .input {width: 700px;height: 200px;border: 1px solid #cccccc;background-color: white;border-radius: 6px;}
        .input .content {width: 690px;height: 150px;margin: 5px;box-sizing: border-box;border: 1px solid gray;resize: none; outline: none; padding: 10px;}
        .input .submit {width: 100px;height: 30px;float: right;background: orangered;color: white;margin-bottom: 5px;margin-right: 5px; border: none; outline: none;cursor: pointer;}
        .dataList {width: 600px;margin: 50px auto;}
        .dataList>ul>li {width: 100%;height: 40px;line-height: 40px;color: slategray;background: lightblue;margin-bottom: 5px;border-radius: 40px;text-indent: 10px;cursor: pointer;}
    </style>
</head>
<body>
<div class="box">
    <div class="input">
        <textarea class="content"></textarea>
        <button class="submit">發佈</button>
    </div>
    <div class="dataList">
        <ul>
            <li>今天的天氣很好呀!</li>
            <li>今天的天氣很好呀!</li>
            <li>今天的天氣很好呀!</li>
        </ul>
    </div>
</div>
<script type="text/javascript" src="lib/jquery-3.3.1.js"></script>
<script type="text/javascript">
    $(function () {
        // 0. 獲取需要的標籤
        var $content = $('.content');
        // 1. 監聽按鈕的點擊
        $('.submit').click(function () {
             // 1.1 獲取輸入的內容
             var content = $content.val();
             // 1.2 容錯
             if($.trim(content).length <= 0){
                 alert('請輸入內容!');
                 //1.2.1 清空輸入框內容
                 $content.val('');
                 // 1.2.2 獲得焦點
                 $('.content').focus();
                 return;
             }
             // 1.3 創建節點
            var $newLi = $('<li>' + content  +'</li>');
            /*$newLi.click(function () {
                $(this).animate({
                    'width': '0px'
                }, 1000, function () {
                    $(this).remove();
                });
            });*/
             // 1.4 追加到ul裏面
            var $ul = $('.dataList>ul');
            $newLi.prependTo($ul);
            // 1.5 設置追加的動畫
            $newLi.hide();
            $newLi.slideDown(500);
            // 1.6 額外的操作
            $content.val('');
            $content.focus();
        });
        // 2. 點擊li標籤, 刪除
        /*$('.dataList>ul>li').click(function () {
             $(this).animate({
                 'width': '0px'
             }, 1000, function () {
                 $(this).remove();
             });
        });*/

       /* $('.dataList>ul').on('click', 'li', function () {
            $(this).animate({
                'width': '0px'
            }, 1000, function () {
                $(this).remove();
            });
        });*/

        $('body').on('click', 'li', function () {
            $(this).animate({
                'width': '0px'
            }, 1000, function () {
                $(this).remove();
            });
        });
    });
</script>
</body>
</html>

運行結果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章