博客列表 >jquery案例第三篇——评论点赞

jquery案例第三篇——评论点赞

温度的博客
温度的博客原创
2019年04月04日 16:42:34843浏览

实例

<!DOCTYPE html>
<html>
<head>
<title>评论功能练习</title>
<meta charset="utf-8">
<link rel="icon" type="image/x-icon" href="static/images/logo.png">
<link rel="stylesheet" type="text/css" href="static/style.css">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div id="list">
    <div class="box clearfix">
<!-- 删除说说按钮 close-->
        <a class="close" href="javascript:;">×</a>
        <img class="head" src="static/images/1.png" alt="">
        <div class="content">
            <div class="main">
                <p class="txt">
                    <span class="user">西门大官人: </span>
                      ~ All the luck is for you. ~
                </p>
                <img class="pic" src="static/images/img1.jpg" alt="">
            </div>
            <div class="info clearfix">
                <span class="time">02-14 23:01</span>
<!-- 给说说点赞 praise -->
                <a class="praise" href="javascript:;">赞</a>
            </div>
            <div class="praises-total" total="4" style="display: block;">4个人觉得很赞</div>
<!--评论内容-->           
            <div class="comment-list">
           <!--   每次评论要添加的内容跟标签   -->
                <div class="comment-box clearfix" user="self">
                    <img class="myhead" src="static/images/4.jpg" alt="">
                    <div class="comment-content">
                        <p class="comment-text"><span class="user">我:</span>你说的都对……</p>
                        <p class="comment-time">
                            2019-01-19 14:36
                            <a href="javascript:;" class="comment-praise" total="0" my="0" style="display: inline-block">赞</a>
                            <a href="javascript:;" class="comment-operate">删除</a>
                        </p>
                    </div>
                </div>
                <!--添加结束-->                                                           
            </div>
<!--评论内容结束--> 
            <div class="text-box">
                    <textarea class="comment" placeholder="评论…"></textarea>
                    <button class="btn">回 复</button>
                    <span class="word"><span class="length">0</span>/140</span>
            </div>                
        </div>            
    </div> 
                   
    <div class="box clearfix">
        <a class="close" href="javascript:;">×</a>
        <img class="head" src="static/images/2.jpg" alt=""/>
        <div class="content">
            <div class="main">
                <p class="txt">
                    <span class="user">欧阳克 : </span>三亚的海滩很漂亮。
                </p>
                <img class="pic" src="static/images/img5.jpg" alt=""/>
            </div>
            <div class="info clearfix">
                <span class="time">02-14 23:01</span>
                <a class="praise" href="javascript:;">赞</a>
            </div>
            <div class="praises-total" total="0" style="display: none;"></div>
            <div class="comment-list">
                <div class="comment-box clearfix" user="other">
                    <img class="myhead" src="static/images/3.png" alt="">
                    <div class="comment-content">
                        <p class="comment-text"><span class="user">韦小宝:</span>我也想去三亚。</p>
                        <p class="comment-time">
                            2019-01-19 14:36
                            <a href="javascript:;" class="comment-praise" total="0" my="0">赞</a>
                            <a href="javascript:;" class="comment-operate">删除</a>
                        </p>
                    </div>
                </div>
            </div>
            <div class="text-box">
                    <textarea class="comment" placeholder="评论…"></textarea>
                    <button class="btn">回 复</button>
                    <span class="word"><span class="length">0</span>/140</span>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function(){
        $('.comment').on('keyup',function(){  //给多行文本域动态绑定一个鼠标事件
            var len = $(this).val().length;   //获取文本域输入的长度值
            $(this).parents('.text-box').find('.length').text(140-len); //在同一个父级下面找到并改变可输入的字数
        })

        //点击回复按钮动态添加评论
        $(document).on('click','.btn',function(){  //在当前文档中找到btn,动态绑定一个点击事件
            var text_val = $(this).prev().val();  // 获取获取文本域的值
            if(text_val == ''){   //验证是否有文字输入
                alert('请输入内容');
            }else{
                //不为空就把评论的html内容块添加到当前评论的后面,其中内容日期为获取变量
                //先获取日期时间
                var date = new Date();
                var time = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+'  ';
                time +=date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();

                //获取文本域内的内容
                var str = $(this).prev().val();
                var html = '<div class="comment-box clearfix" user="other"><img class="myhead" src="static/images/3.png" alt=""><div class="comment-content"><p class="comment-text"><span class="user">韦小宝:</span>'+str+'</p><p class="comment-time">'+time+'<a href="javascript:;" class="comment-praise" total="0" my="0">赞</a><a href="javascript:;" class="comment-operate">删除</a></p></div></div>';

                // 找到同一个父级下面的comment-list,在其后面添加html评论块
                $(this).parents('.content').find('.comment-list').append(html);

                //初始化文本域内容
                $(this).parents('.text-box').find('.comment').val('');

                //初始化可输入字数
                $(this).parents('.text-box').find('.length').text('0');

                //初始化输入框高度
                $(this).prev().height(20);
                //console.log(html);
            }
        })

        //动态删除评论
        $(document).on('click','.comment-operate',function(){
            if(confirm('确定删除吗?')){
                $(this).parents('.comment-box').remove();
            }
        })

        //点击输入框改变高度
        $('.comment').on('click',function(){
            $(this).height(80);
        })

        //点赞+1
        $(document).on('click','.comment-praise',function(){
            var num = Number($(this).attr('total'))+1;
            $(this).attr('total',num);
            $(this).html(num+'赞');
            console.log(num);
        })

    })
</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议
温度。2019-07-04 14:48:452楼
sdasdasd
温度。2019-04-08 16:23:531楼
点赞图片变化,底下的数字也随着+1;整个说说删除;输入字数为负变个色,不能提交;