" />

最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一篇文章告訴你如何用事件委托實現(xiàn)JavaScript留言板功能

 更新時間:2021年12月27日 09:53:33   作者:小高今天早睡了嗎?  
這篇文章主要為大家介紹了事件委托實現(xiàn)JavaScript留言板功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

用事件委托實現(xiàn)留言板功能。

在這里插入圖片描述

在這里插入圖片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        body {
            width: 100%;
            height: 100%;
            background: rgb(65, 65, 63);
        }
        .bacground {
            width: 700px;
            height: 100%;
            background: white;
            margin: auto;
            margin-top: 20px;
        }
        .head,
        .pop-head {
            height: 50px;
            font-size: 20px;
            text-align: center;
            line-height: 50px;
        }
        .name {
            width: 640px;
            height: 40px;
            font-size: 20px;
            margin: 10px 28px;
            line-height: 50px;
            border-radius: 8px;
            border: 2px solid rgb(139, 137, 137);
            outline: none;
        }
        .content,
        .pop-reply {
            width: 640px;
            height: 150px;
            font-size: 22px;
            margin: 10px 28px;
            border: 2px solid rgb(139, 137, 137);
            outline: none;
            border-radius: 8px;
            resize: none;
        }
        .btn,
        .pop-btn {
            float: right;
            height: 30px;
            margin-right: 28px;
            border-radius: 6px;
            outline: none;
            font-size: 20px;
            padding: 0 20px;
            background: rgb(169, 238, 255);
        }
        h3 {
            font-size: 20px;
            color: rgb(102, 102, 102);
            background: rgb(205, 221, 248);
            margin-top: 50px;
            line-height: 50px;
            text-indent: 30px;
            font-weight: 545;
        }
        li {
            list-style: none;
            width: 640px;
            font-size: 22px;
            margin: 15px 30px;
        }
        .message-head {
            display: flex;
        }
        .message-head .photo {
            width: 70px;
            height: 70px;
            background: rgb(6, 109, 134);
            display: inline-block;
            border-radius: 50%;
            text-align: center;
            line-height: 70px;
            overflow: hidden;
        }
        .message-head .time {
            margin-left: 12px;
        }
        .liuyan,
        .reply p {
            width: 560px;
            /* height: 76px; */
            line-height: 50px;
            display: block;
            background: rgb(205, 221, 248);
            font-size: 20px;
            margin-left: 80px;
            border-radius: 8px;
            text-indent: 15px;
        }
        .delete {
            width: 60px;
            height: 30px;
            display: block;
            line-height: 30px;
            margin-left: 580px;
            /* margin-bottom: 0px; */
            border-radius: 6px;
            outline: none;
            font-size: 15px;
            background: rgb(169, 238, 255);
        }
        .answer {
            width: 60px;
            height: 30px;
            display: block;
            line-height: 30px;
            margin-top: -29px;
            margin-left: 515px;
            border-radius: 6px;
            outline: none;
            font-size: 15px;
            background: rgb(169, 238, 255);
        }
        .popup {
            width: 100vw;
            height: 100vh;
            position: fixed;
            background: rgba(0, 0, 0, .3);
            left: 0;
            top: 0;
            z-index: 10;
            display: flex;
            align-items: center;
            justify-content: center;
            display: none;
        }
        .pop-content {
            width: 700px;
            background: #fff;
            border-radius: 10px;
            overflow: hidden;
            padding-bottom: 20px;
        }
        .reply p {
            margin-top: 10px;
            background: rgba(100, 100, 100, .1);
        }
    </style>
</head>
<body>
    <div class="bacground">
        <div class="head">留言板</div>
        <input class="name" type="text" placeholder="請輸入您的昵稱">
        <textarea class="content" placeholder="請保持言論文明......"></textarea>
        <button class="btn">留言</button>
        <h3>大家在說</h3>
        <ul class="text">
            <!-- <li>
			<div class="message-head">
			<span class="photo">系統(tǒng)</span>
			<p class="time">2019-9-27 0:47:38</p>
			</div>
			<p class="liuyan">測試留言</p>
			<div class="reply">
			<p>測試回復(fù)</p>
			</div>
			<button class="delete">Delete</button>
			<button class="answer">Answer</button>
		</li> -->
        </ul>
    </div>
    <div class="popup">
        <div class="pop-content">
            <div class="pop-head">回復(fù)板</div>
            <textarea class="pop-reply" placeholder="請保持言論文明......"></textarea>
            <button class="pop-btn huiFu">回復(fù)</button>
            <button class="pop-btn quXiao">取消</button>
        </div>
    </div>
    <script>
        //在事件委托中,每一個if都相當于一個獨立的函數(shù),因為每一次點擊都會重新觸發(fā)事件處理函數(shù)
        var oAns ;
        //分析:事件委托給誰? --- 共同的父元素
        document.onclick = function (e) {
            //事件處理對象,兼容IE8及以下版本的瀏覽器
            e = e || event ;
            // target目標  --- 具體是由哪個標簽觸發(fā)的
            var target = e.target ;

            //留言
            if(target.className === 'btn'){
                //獲取對象
                var nickname = $('.name').value ;
                var content = $('.content').value ;
                //判斷輸入是否為空
                if(nickname && content){
                    //創(chuàng)建一個標簽
                    var oLi = document.createElement('li') ;
                    //插入新內(nèi)容
                    oLi.innerHTML = `
                    <div class="message-head">
                        <span class="photo">${nickname}</span>
                        <p class="time">2019-9-27 0:47:38</p>
                    </div>
                    <p class="liuyan">${content}</p>
                    <div class="reply">
                    </div>
                    <button class="delete">Delete</button>
                    <button class="answer">Answer</button>
                    `  
                    //將最新的留言插入到最上面
                    $('.text').insertBefore(oLi , $('.text').firstChild) ;
                    //倒計時
                    clock(target , 3) ;
                    //留言完后清空留言板內(nèi)容
                    $('.content').value = '' ;
                }else{
                    alert('輸入不能為空!')
                }
                return
            }
            //刪除
            if(target.className === 'delete'){
                //刪除留言
                target.parentNode.remove() ;
                return
            }
            //回復(fù)
            if(target.className === 'answer'){
                //顯示彈窗
                $('.popup').style.display = 'flex' ;
                //找到回復(fù)留言的地方
                oAns = target.previousElementSibling.previousElementSibling ;
                return 
            }
            //確認回復(fù)
            if(target.className === 'pop-btn huiFu'){
                //拿到回復(fù)的內(nèi)容
                var huiFuContent = $('.pop-reply').value ;
                if(huiFuContent){
                    //創(chuàng)建一個標簽
                    var oP = document.createElement('p') ;
                    //將內(nèi)容插入標簽中
                    oP.innerHTML = huiFuContent ;
                    //將回復(fù)插入到留言的地方
                    oAns.appendChild(oP) ;
                }
                //關(guān)閉彈窗
                $('.popup').style.display = 'none' ;
                return
            }
            //取消回復(fù)
            if(target.className === 'pop-btn quXiao'){
                $('.popup').style.display = 'none' ;
                return
            }
        }
        //倒計時
        function clock(ele , time){
            if(!ele.disabled){
                ele.disabled = true ;
                ele.innerHTML = time + 's后可再次留言' ;
                var t = setInterval(function () {
                    time-- ;
                    ele.innerHTML = time + 's后可再次留言' ;
                    if(time <= 0){
                        clearInterval(t) ;
                        ele.disabled = false ;
                        ele.innerHTML = '留言' ;
                    }
                },1000)
            }
        }
        //獲取對象
        function $(selector){
            return document.querySelector(selector) ;
        }
    </script>
</body>
</html>

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • 前端日歷插件VCalendar的簡單使用方法

    前端日歷插件VCalendar的簡單使用方法

    這篇文章主要介紹了VCalendar的安裝和使用方法,包括如何通過官網(wǎng)地址引入API,并在項目中全局引入main.js文件,使用VCalendar可以實現(xiàn)日歷功能,需要的朋友可以參考下
    2025-01-01
  • js 判斷一個元素是否在頁面中存在

    js 判斷一個元素是否在頁面中存在

    js 判斷一個元素是否存在此功能在項目中很實用,本人搜集整理了一些常用技巧,需要了解的朋友可以參考下
    2012-12-12
  • javascript中in運算符用法分析

    javascript中in運算符用法分析

    這篇文章主要介紹了javascript中in運算符用法,實例分析了in運算符的相關(guān)使用技巧,非常具有實用價值,需要的朋友可以參考下
    2015-04-04
  • js的函數(shù)的按值傳遞參數(shù)(實例講解)

    js的函數(shù)的按值傳遞參數(shù)(實例講解)

    下面小編就為大家分享一篇js的函數(shù)的按值傳遞參數(shù)的實例,具有很好的參考價值,一起跟隨小編過來看看吧,希望對大家有所幫助
    2017-11-11
  • 基于JS實現(xiàn)省市聯(lián)動效果代碼分享

    基于JS實現(xiàn)省市聯(lián)動效果代碼分享

    這篇文章主要介紹了基于JS實現(xiàn)省市聯(lián)動效果代碼的相關(guān)資料,非常實用,在日常項目開發(fā)過程中經(jīng)常遇到此需求,下面小編給大家分享實現(xiàn)代碼,需要的朋友可以參考下
    2016-06-06
  • 微信小程序之裁剪圖片成圓形的實現(xiàn)代碼

    微信小程序之裁剪圖片成圓形的實現(xiàn)代碼

    最近在開發(fā)小程序,產(chǎn)品經(jīng)理提了一個需求,要求微信小程序換頭像,用戶剪裁圖片必須是圓形。這篇文章主要介紹了微信小程序之裁剪圖片成圓形 ,需要的朋友可以參考下
    2018-10-10
  • 詳解webpack+gulp實現(xiàn)自動構(gòu)建部署

    詳解webpack+gulp實現(xiàn)自動構(gòu)建部署

    這篇文章主要介紹了詳解webpack+gulp實現(xiàn)自動構(gòu)建部署,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • js 回車提交表單兩種實現(xiàn)方法

    js 回車提交表單兩種實現(xiàn)方法

    js 回車提交一些新手朋友還是比較陌生的,本文介紹兩種實現(xiàn)方法:jQuery方法、JavaScript方法,感興趣的朋友可以研究下
    2012-12-12
  • 移動前端圖片壓縮上傳的實例

    移動前端圖片壓縮上傳的實例

    下面小編就為大家分享一篇移動前端圖片壓縮上傳的實例。具有很好的參考價值,希望對大家有所幫助。一起小編過來看看吧
    2017-12-12
  • layui問題之模擬select點擊事件的實例講解

    layui問題之模擬select點擊事件的實例講解

    今天小編就為大家分享一篇layui問題之模擬select點擊事件的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08

最新評論

大悟县| 东宁县| 东宁县| 娄底市| 昆明市| 石屏县| 朝阳县| 武安市| 龙州县| 盖州市| 从化市| 孟州市| 盘山县| 清新县| 新干县| 武宣县| 丰宁| 独山县| 伊金霍洛旗| 奉节县| 仙桃市| 尼勒克县| 宜良县| 英吉沙县| 扶沟县| 武鸣县| 永胜县| 西乌珠穆沁旗| 漾濞| 屏东市| 义马市| 陕西省| 永胜县| 隆昌县| 昌平区| 二连浩特市| 汨罗市| 大田县| 永川市| 镇巴县| 昭通市|