div使用contenteditable兼容问题

1、div可以使用contenteditable模拟input框
在vue中,可以使用v-model实现组件的双向绑定,组件中使用v-html显示值
2、可以给div添加tabindex,使div支持focus和blur事件
3、可以使用css3实现placeholder

 /* 当div为空时,显示data-placeholder的值*/
.input-text-placeholder:empty:before {
  content: attr(data-placeholder);
  color: #cacaca;
}

4、ios无法输入问题
可以使用-webkit-user-select: text;解决

.ios-user-select {
  -webkit-user-select: text;
}

5、在使用了input事件后,光标位置显示不正常问题

<div
  ref="input"
  :name="attr"
  contenteditable="true"
  tabindex="1"
  class="input-text ios-user-select input-text-placeholder"
  v-html="currentValue"
  :data-placeholder="placeholder"
  @blur="checkInput"
  @input="edit">
</div>
// 监听中文输入
mounted () {
  // 监听中文输入
  const el = this.$refs.input
  el.addEventListener('compositionstart', this.onCompositionstart, false)
  el.addEventListener('compositionend', this.onCompositionend, false)
},
methods: {
  onCompositionstart (e) {
    this.isLock = true
  },
  onCompositionend (e) {
    this.isLock = false
  },
  edit (e) {
    // 解决中文输入的时候,直接输出英文字母的问题(中文输入期间,不允许输入字符)
    setTimeout(() => {
      if (this.isLock) return
      let value = e.target.innerHTML
      // 去除换行符
      value = value.replace(/(\<\/?\w*\>)/g, '').replace(/(\r\n)|(\n)/g, '')
      this.$emit('input', {
        attr: this.attr,
        value: value
      })
    }, 0)
    // 如果是pc的话,timeout设置为5就好,ios经测试,使用40才有效果
    const timeout = 40
    setTimeout(() => {
      this.getCursor(e.target)
    }, timeout)
  },
  // 解决光标定位问题
  getCursor (el) {
    if (window.getSelection) {
      el.focus()
      const sel = getSelection()
      sel.selectAllChildren(el)
      sel.collapseToEnd()
    } else if (document.selection) {
      var range = document.selection.createRange()// 创建选择对象
      range.moveToElementText(el)// range定位到el
      range.collapse(false)// 光标移至最后
      range.select()
    }
  }
},
beforeDestroy () {
  // 清除监听中文输入
  const el = this.$refs.input
  el.removeEventListener('compositionstart', this.onCompositionstart, false)
  el.removeEventListener('compositionend', this.onCompositionend, false)
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Mobile Web Favorites 参与贡献 移动前端开发收藏夹,欢迎使用Issues以及 Pull Req...
    柴东啊阅读 772评论 0 2
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,736评论 1 45
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,053评论 0 2
  • 看到首页上赫然出现的字体:简书再见。最近开始发觉简书上打卡已经是一种习惯了。对于我,无论好坏,都不想放弃。 翻开简...
    七月上的冥王星阅读 129评论 0 1
  • 香飘千里奇迹感恩日记 2018.10.9 一、奇迹 1、我班的女孩子用几节课的功夫编好了节目卡路里,真让我高兴哦!...
    香飘千里8阅读 137评论 0 0