手把手教你用JS实现回车评论功能

 更新时间:2023年06月16日 16:29:34   作者:几何小超  
最近在写一个问答功能,类似于评论,下面这篇文章主要给大家介绍了关于如何用JS实现回车评论功能的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

首先我们来制作b站的回车框

HTML部分

 <div class="wrapper">
    <i class="avatar"></i>
    <textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
    <button>发布</button>
  </div>
  <div class="wrapper">
    <span class="total">0/200字</span>
  </div>
  <div class="list">
    <div class="item" style="display: none;">
      <i class="avatar"></i>
      <div class="info">
        <p class="name">清风徐来</p>
        <p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]</p>
        <p class="time">2022-10-10 20:29:21</p>
      </div>
    </div>
  </div>

然后就是我们的css部分,我们要实现点击文本框使文本框进行均匀下拉的操作,可以使用focus伪类选择器,

focus是css的一个伪类选择器,可以用来选取获得焦点的元素,然后为这些获得焦点的元素设置样式。

只要是可以接收键盘事件或其他用户输入的元素都可以:focus选择器,大多数情况下:focus选择器都是被使用在链接和表单元素上的。

然后再下面添加运动的路径即可

.wrapper {
      min-width: 400px;
      max-width: 800px;
      display: flex;
      justify-content: flex-end;
    }
 
    .avatar {
      width: 48px;
      height: 48px;
      border-radius: 50%;
      overflow: hidden;
      background: url(./images/avatar.jpg) no-repeat center / cover;
      margin-right: 20px;
    }
 
    .wrapper textarea {
      outline: none;
      border-color: transparent;
      resize: none;
      background: #f5f5f5;
      border-radius: 4px;
      flex: 1;
      padding: 10px;
      transition: all 0.5s;
      height: 30px;
    }
 
    .wrapper textarea:focus {
      border-color: #e4e4e4;
      background: #fff;
      height: 50px;
    }
 
    .wrapper button {
      background: #00aeec;
      color: #fff;
      border: none;
      border-radius: 4px;
      margin-left: 10px;
      width: 70px;
      cursor: pointer;
    }
 
    .wrapper .total {
      margin-right: 80px;
      color: #999;
      margin-top: 5px;
      opacity: 0;
      transition: all 0.5s;
    }
 
    .list {
      min-width: 400px;
      max-width: 800px;
      display: flex;
    }
 
    .list .item {
      width: 100%;
      display: flex;
    }
 
    .list .item .info {
      flex: 1;
      border-bottom: 1px dashed #e4e4e4;
      padding-bottom: 10px;
    }
 
    .list .item p {
      margin: 0;
    }
 
    .list .item .name {
      color: #FB7299;
      font-size: 14px;
      font-weight: bold;
    }
 
    .list .item .text {
      color: #333;
      padding: 10px 0;
    }
 
    .list .item .time {
      color: #999;
      font-size: 12px;
    }

接下来就是我们JS部分

我来解析一下下面的思路

还是第一步我们先得获取里面的元素,只有这样才能进行接下来的操作

然后我们来写文本焦点:tx这个文本域制作一个事件,这个事件是获得焦点,让下面的0/200字的文字显示出来

tx.addEventListener('focus',function(){
      total.style.opacity=1
    })

当我们点击空白的时候得隐藏起来,同意复制上面的代码只需要改成blur就行

tx.addEventListener('blur',function(){
      total.style.opacity=0
    })

3.然后我们得做一个监听事件,检测用户输入多少字,然后让下面的0/200行来跟着用户变化

    tx.addEventListener('focus',function(){
      total.innerHTML=`${tx.value.length}/200字`
    })

第四步我们开始正式操作

实现按下回车发布评论

这里的keyup是最新版语法已经淘汰之前的keycoad,意思是键盘弹出事件,然后这里的e在keyup里面是回车的意思,所以要写在方法里面

然后我们进行判断,如果用户输入不为空就会显示打印,这边的trim是消除两边空格的意思

item.style.display='block'
            text.innerHTML=tx.value

意思是显示文本中的内容,然后最后等我们按下回车结束后,就得清空文本域,所以我们的tx的最后得为空,然后发现我们下面的没有复原,这时候我们还需要加total.innerHtml='0/200字'才能复原成之后的样子

    tx.addEventListener('keyup',function(e){
        //只有按下回车才会触发
        if(e.key === 'Enter'){
            //如果用户输入不为空就会显示打印
            if(tx.value.trim()!==''){
                item.style.display='block'
            text.innerHTML=tx.value
            }
            //等我们按下回车,结束后,清空文本域
            tx.value=''
            //按下回车就得复原
            total.innerHTML='0/200字'
        }
    })

这是JS源码

<script>
    const tx=document.querySelector('#tx');
    const total=document.querySelector('.total')
    const item=document.querySelector('.item');
    const text=document.querySelector('.text')
    //1.当我们文本获得焦点,让total显示出来
    tx.addEventListener('focus',function(){
      total.style.opacity=1
    })
    //1.当我们文本失去焦点,让total隐藏出来
    tx.addEventListener('blur',function(){
      total.style.opacity=0
    })
    // 3.监测用户输入
    tx.addEventListener('focus',function(){
      total.innerHTML=`${tx.value.length}/200字`
    })
    //4.按下回车发布评论
    tx.addEventListener('keyup',function(e){
        //只有按下回车才会触发
        if(e.key === 'Enter'){
            //如果用户输入不为空就会显示打印
            if(tx.value.trim()!==''){
                item.style.display='block'
            text.innerHTML=tx.value
            }
            //等我们按下回车,结束后,清空文本域
            tx.value=''
            //按下回车就得复原
            total.innerHTML='0/200字'
        }
    })
  </script>

还是老规矩,为了大家看的清除,我一边进行学习,一边写注释让我更加明白进行的操作,也是为了方便写文章

<!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>评论回车发布</title>
  <style>
    .wrapper {
      min-width: 400px;
      max-width: 800px;
      display: flex;
      justify-content: flex-end;
    }
    .avatar {
      width: 48px;
      height: 48px;
      border-radius: 50%;
      overflow: hidden;
      background: url(./images/avatar.jpg) no-repeat center / cover;
      margin-right: 20px;
    }
    .wrapper textarea {
      outline: none;
      border-color: transparent;
      resize: none;
      background: #f5f5f5;
      border-radius: 4px;
      flex: 1;
      padding: 10px;
      transition: all 0.5s;
      height: 30px;
    }
    .wrapper textarea:focus {
      border-color: #e4e4e4;
      background: #fff;
      height: 50px;
    }
    .wrapper button {
      background: #00aeec;
      color: #fff;
      border: none;
      border-radius: 4px;
      margin-left: 10px;
      width: 70px;
      cursor: pointer;
    }
    .wrapper .total {
      margin-right: 80px;
      color: #999;
      margin-top: 5px;
      opacity: 0;
      transition: all 0.5s;
    }
    .list {
      min-width: 400px;
      max-width: 800px;
      display: flex;
    }
    .list .item {
      width: 100%;
      display: flex;
    }
    .list .item .info {
      flex: 1;
      border-bottom: 1px dashed #e4e4e4;
      padding-bottom: 10px;
    }
    .list .item p {
      margin: 0;
    }
    .list .item .name {
      color: #FB7299;
      font-size: 14px;
      font-weight: bold;
    }
    .list .item .text {
      color: #333;
      padding: 10px 0;
    }
    .list .item .time {
      color: #999;
      font-size: 12px;
    }
  </style>
</head>
<body>
  <div class="wrapper">
    <i class="avatar"></i>
    <textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
    <button>发布</button>
  </div>
  <div class="wrapper">
    <span class="total">0/200字</span>
  </div>
  <div class="list">
    <div class="item" style="display: none;">
      <i class="avatar"></i>
      <div class="info">
        <p class="name">清风徐来</p>
        <p class="text">大家都辛苦啦,感谢各位大大的努力,能圆满完成真是太好了[笑哭][支持]</p>
        <p class="time">2022-10-10 20:29:21</p>
      </div>
    </div>
  </div>
  <script>
    const tx=document.querySelector('#tx');
    const total=document.querySelector('.total')
    const item=document.querySelector('.item');
    const text=document.querySelector('.text')
    //1.当我们文本获得焦点,让total显示出来
    tx.addEventListener('focus',function(){
      total.style.opacity=1
    })
    //1.当我们文本失去焦点,让total隐藏出来
    tx.addEventListener('blur',function(){
      total.style.opacity=0
    })
    // 3.监测用户输入
    tx.addEventListener('focus',function(){
      total.innerHTML=`${tx.value.length}/200字`
    })
    //4.按下回车发布评论
    tx.addEventListener('keyup',function(e){
        //只有按下回车才会触发
        if(e.key === 'Enter'){
            //如果用户输入不为空就会显示打印
            if(tx.value.trim()!==''){
                item.style.display='block'
            text.innerHTML=tx.value
            }
            //等我们按下回车,结束后,清空文本域
            tx.value=''
            //按下回车就得复原
            total.innerHTML='0/200字'
        }
    })
  </script>
</body>
</html>

总结

到此这篇关于用JS实现回车评论功能的文章就介绍到这了,更多相关JS回车评论功能内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • js定义类的几种方法(推荐)

    js定义类的几种方法(推荐)

    下面小编就为大家带来一篇js定义类的几种方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-06-06
  • 原生js调用json方法总结

    原生js调用json方法总结

    本篇文章给大家详细分析了js调用json方法的总结,对此有需要的朋友可以参考学习下。
    2018-02-02
  • JS点击缩略图整屏居中放大图片效果

    JS点击缩略图整屏居中放大图片效果

    今天开发的时候,遇到要点击缩略图之后居中显示图片的大图查看(大致效果如上图所示)~想了好几种实现方式,下面通过本文给大家分享JS点击缩略图整屏居中放大图片效果,需要的朋友参考下吧
    2017-07-07
  • javascript自启动函数的问题探讨

    javascript自启动函数的问题探讨

    自启动函数想必大家并不陌生吧,在本文将为大家详细探讨下,感兴趣的朋友可不要错过
    2013-10-10
  • 基于JS实现翻书效果的页面切换样式

    基于JS实现翻书效果的页面切换样式

    在项目开发中经常遇到翻书的页面切换效果,基于js代码怎么实现的呢?今天小编给大家分享基于JS实现翻书效果的页面切换样式,需要的朋友参考下吧
    2017-02-02
  • 用Three.js实现3D圆环图的思路及实例代码

    用Three.js实现3D圆环图的思路及实例代码

    作为一个数据可视化中经久不衰的经典图表,3D饼图不仅能更直观地展示数据比例关系,还能通过立体的视觉效果增强整体的视觉冲击力,这篇文章主要介绍了用Three.js实现3D圆环图的思路及实例代码,需要的朋友可以参考下
    2026-02-02
  • 微信小程序时间标签和时间范围的联动效果

    微信小程序时间标签和时间范围的联动效果

    这篇文章主要为大家详细介绍了微信小程序时间标签和时间范围的联动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • uniapp中实现App自动检测版本升级的示例代码

    uniapp中实现App自动检测版本升级的示例代码

    本文主要介绍了uniapp中实现App自动检测版本升级的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-01-01
  • JS 添加网页桌面快捷方式的代码详细整理

    JS 添加网页桌面快捷方式的代码详细整理

    如何添加桌面快捷?很多网友都有这个疑问;JS 点击添加网页桌面快捷方式的代码,需要的朋友可以参考下
    2012-12-12
  • javascript定义函数的方法

    javascript定义函数的方法

    javscript中定义和声明函数有三种方式:正常方法 构造函数 函数直接量。
    2010-12-12

最新评论