Blogger Information
Blog 20
fans 0
comment 0
visits 25105
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
04-03js仿评论功能页面功能
左手Leon的博客
Original
590 people have browsed it

实例

<!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 type="text/javascript" src="static/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="1" my="0" style="display: inline-block">1赞</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>
    $(function(){
        //on()动态绑定
        $('.comment').on('keyup',function(){
            var len=$(this).val().length;
            $(this).parents('.text-box').find('.length').text(140-len);
        })
        $(document).on('click','.btn',function(){
        // $(document).on('click','.btn',function(){
            //紧邻的同胞元素
            var val=$(this).prev().val()
            if(val==''){
                alert('评论不能为空!')
            }else{
                var date=new Date()
                var y=date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+'   '+date.getHours()+':'+date.getMinutes()+':'+date.getSeconds();
                console.log(y);
                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>'+val+'</p><p class="comment-time">'+y+'<a href="javascript:;" class="comment-praise" total="0" my="0">赞</a><a href="javascript:;" class="comment-operate">删除</a></p></div> </div>' 
                $(this).parents('.box').find('.comment-list').append(html);
                $(this).parents('.box').find('.comment').val('');//清空内容
                $(this).parents('.box').find('.length').text('0');//初始化输入字数
                $(this).prev().height(20)
            }

        })
        //动态删除
        // $('.comment-operate').on('click',function(){
        //     console.log(y);
            // if(confirm('确定删除么?')){
            //     $(this).parents('.comment-box').remove();
            // }
        // })
        

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

        //删除帖子
         $(document).on('click','.close',function(){
            $(this).parents('.box').remove();
         })


        //点击评论区后,增加评论区高度
        $(document).on('click','.comment',function(){
             $(this).height(80);
        })

        //评论点赞
         $(document).on('click','.comment-praise',function(){
             var num=parseInt($(this).attr('total'))+1;
             $(this).attr('total',num);
             if(num>0){
                $(this).attr('style','display:inline-block')
             }
             
             $(this).html(num+'赞')
        })

         //帖子点赞
         $(document).on('click','.praise',function(){
            // var that=this;
             var count=parseInt($(this).parents('.info').siblings('.praises-total').attr('total'))+1;
           
             console.log(this)
             $(this).parents('.info').siblings('.praises-total').attr({'total':count,'style':'diplay:block'});
             
             $(this).parents('.info').siblings('.praises-total').html(count+'个人觉得很赞')
              console.log(this)
        })
         


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

运行实例 »

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

心得:

1、要注意各级别元素的查找方式

查找父级的同级,用到parent()和sibling()

2、元素本身的属性如显示为何各块级,用jquery书写时要为attr('style','display:inline-block')

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!