回车或按钮事件评论

练习到这里,分享出来,虽然还有许多不足。

回车按钮评论事件

1.文档内容

textarea 设置只读性,限制最大字数

 <div class="bigBox">
    <!-- maxlengt 最大字数限制 -->
    <textarea name="" id="text" placeholder="发一条评论吧~" rows="" maxlength="200"></textarea>

    <button class="btn">发布</button>

  </div>
  <div class="box1">
    <p>0/200字</p>


    <br>
    <div class="box2">
      <img src="./新的.png" alt="" srcset="" class="img1" style="width: 150px; height: 150px; ">
      <!-- 设置只读性 -->
      <textarea class="box3" readonly></textarea>

    </div>

  </div>

2.JS部分

  <script>
    // button按钮
    const btn = document.querySelector('.btn')
    // 发布评论区
    const textarea = document.querySelector('#text')
    // 字数
    const p1 = document.querySelector('.box1 p')
    //  评论区
    let box3 = document.querySelector('.box3')
    // 头像
    let img1 = document.querySelector('.img1')


    // 获取焦点显示字数
    textarea.addEventListener('focus', function () {
      p1.style.display = 'block'

    })
    // 失去焦点
    textarea.addEventListener('blur', function () {
      p1.style.display = 'none'
    })

    // 点击按钮
    btn.addEventListener('click', function () {
      console.log(textarea.value.length)
      // 评论字数
      let if1 = textarea.value.length = '0'
      let if2 = textarea.value.length != '0'

      // 如果字数为0,隐藏头像和评论区
      if (if1) {
        box3.style.opacity = '0'
        img1.style.opacity = '0'
      }
      // 字数不为0,显示评论区
      if (if2) {
        box3.style.opacity = '1'
        // 评论区文本
        box3.innerHTML = textarea.value
        img1.style.opacity = '1'
        // 发布评论区清空
        textarea.value = ''

      }



    })

    // 按下回车键与点击按钮事件相同
    textarea.addEventListener('keyup', function (e) {
      if (e.key === 'Enter') {
        e.preventDefault()
        console.log(e.key)
        let if1 = textarea.value.length = '0'
        let if2 = textarea.value.length != '0'

        if (if1) {
          box3.style.opacity = '0'
          img1.style.opacity = '0'
        }
        if (if2) {
          box3.style.opacity = '1'
          box3.innerHTML = textarea.value
          img1.style.opacity = '1'
          textarea.value = ''

        }

      }

    })

    // 监听输入文字
    textarea.addEventListener('input', function () {

      let num = textarea.value.length
      p1.innerHTML = `${num}/200字`
    })
  </script>

3.样式

过渡效果,textarea去掉下拉条,禁止调整尺寸,移除聚焦样式

<style>
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    .bigBox {
      display: flex;

    }

    [id=text] {
      padding: 10px;
      margin-top: 20px;
      margin-left: 150px;
      width: 300px;
      height: 150px;
      padding-bottom: 70px;
      border: 2px rgb(149, 188, 208) solid;
      /*过渡效果 */
      transition: all .3s;
      border-radius: 10px;
      font-size: 15px;
      resize: none;
      background-color: rgb(236, 245, 252);
      /* 去掉下拉条 */
      overflow: hidden;




    }

    [id=text]:focus {
      /*  移除默认聚焦样式*/
      outline: none;
      outline-color: rgb(134, 212, 248);
      width: 400px;
      background-color: transparent;



    }

    button {
      color: white;
      margin: 75px 20px 0px;
      width: 50px;
      height: 50px;
      border-style: none;
      border-radius: 10px;
      cursor: pointer;
      background-color: rgb(134, 212, 248);
    }

    button:hover {
      background-color: rgb(170, 214, 236);
    }

    .box1 p {
      margin-left: 500px;
      font-size: 13px;
      display: none;
    }

    .box2 {
      display: flex;
      padding-top: 50px;
    }

    .box3 {
      margin-left: 0;
      width: 300px;
      height: 150px;
      border: 2px rgb(136, 213, 255) solid;
      border-radius: 10px;
      font-size: 15px;
      opacity: 0;
      /* 禁止调整尺寸 */
      resize: none;
      /* 移除聚焦样式 */
      outline: none;
      margin-top: 40px;
      padding: 10px;
      /* 去掉下拉条 */
      overflow: hidden;
    }

    .img1 {
      opacity: 0;
    }
  </style>

这是自用代码图片

  • 17
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值