zoukankan      html  css  js  c++  java
  • vue中使用 contenteditable 制作输入框并使用v-model做双向绑定

    <template>
      <div class="div-input"
           :class="value.length > 0 ? 'placeholder_hide' : ''"
           :style="{'min-width': minWidth}"
           :contenteditable="input"
           :placeholder="placeholder"
           @focus="ischecked = true"
           @blur="blurFn"
           @input="inputFn"
           v-html="text"></div>
    </template>
    
    <script>
      export default {
        name: 'DivInput',
        props: {
          input: {
            type: Boolean,
            default: true
          },
          minWidth: {
            type: String,
            default: '2.2em'
          },
          placeholder: {
            type: String,
            default: 'asdasdasda'
          },
          value: {
            type: String,
            default: ''
          }
        },
        data () {
          return {
            ischecked: false,
            text: this.value
          }
        },
        mounted () {
        },
        watch: {
          value () {
            // 解决光标跳动BUG
            if (!this.ischecked) {
              this.text = this.value
            }
          }
        },
        methods: {
          inputFn (e) {
            const val = e.target.innerHTML
            this.$emit('input', val)
          },
          blurFn (e) {
            this.ischecked = false
            this.text = this.value
            e.view.blur()
          }
        }
      }
    </script>
    
    <style lang="scss">
      .div-input {
        display: inline-block;
        min- 4em;
        border-bottom: 1px solid #ccc;
        outline: none;
        padding: 0 5px;
        &:focus{
          border-color: #333;
        }
        &:before {
          content: attr(placeholder);
          white-space: nowrap;
           100%;
          color: #aaa;
          z-index: 0;
          cursor: text;
        }
        &.placeholder_hide {
          &:before {
            display: none;
          }
        }
      }
    </style>
    
  • 相关阅读:
    兰迪·波许教授的最后一课
    How can I convert from GdiPlus::Image to CBitmap?
    环境变量以及ControlSet
    判断操作系统类型的多种方法
    顺序表
    操作系统版本号
    64bit操作系统的重定向
    CListCtrl资料
    Custom draw 和 Owner draw 的区别
    Silent Install / Uninstall
  • 原文地址:https://www.cnblogs.com/lvyueyang/p/9554416.html
Copyright © 2011-2022 走看看