zoukankan      html  css  js  c++  java
  • 实现输入框换行

    应产品要求,实现输入框换行功能,但是传统的input不支持换行操作,故而使用其他方式进行实现。

    方式一:contenteditable属性的使用

    使用一个div,<div contenteditable="true" id="t"></div>

    但是存在问题是如果配合vue的双向绑定会报错

    方式二:使用textarea进行模拟,监听内容变化,动态改变其高度

    html:

    <textarea  class="input-item textarea-item"   placeholder="请输入详细地址"  
          rows="1"  ref="address" 
      @focus="showClearBtn('AddressDetail')" 
      @blur="hideCleanBtn('AddressDetail')"   
      v-model="resObj.address_detail">
    </textarea>
    

    JS:

     watch: {
        'resObj.address_detail': function (newVal) {
          this.getHeight(this.$refs.address)
        },
      },
    // 详细地址输入框高度
    getHeight (el) {
    this.timer = setTimeout(() => {
    el.style.height = 'auto' // 必须设置为auto
    el.style.height = (el.scrollHeight) + 'px'
    }, 20)
    },
  • 相关阅读:
    基础DP背包
    哲学思絮01
    Vue使用ElementUI
    Vue-Mock数据
    Vue生命周期
    Vue实战之CURD
    读《间客》有感
    ASP.Net Core网站发布
    Cycling之 标签化
    Vue环境搭建
  • 原文地址:https://www.cnblogs.com/evaling/p/9265098.html
Copyright © 2011-2022 走看看