contenteditable 仿造input 光标定位

背景:在初次使用contenteditable写可输入的div模块,当需求要求输入限制字数,还要光标正常的情况来模拟input

直接上demo,用vue写的(框架不重要),demo中用了vant的toast,css注意下是sass

如果你了解vue,可以直接搭建一个demo项目,然后复制下下面的代码运行起来就能看到效果
搭建demo可以参考我之前的文章https://segmentfault.com/a/11...

<template>
  <div class="edit-content-wrap">
    <div
      class="edit-content thin-scroll"
      contenteditable="true"
      ref="edit-content"
      placeholder="Ctrl+V 可粘贴文字"
      @keydown="inputChecked"
      @keyup="inputLength"
    >{{ comm_info }}</div>
    <div class="edit-footer">
      <div class="edit-footer-part-3">
        <span>{{ currentLength }}</span>
        /
        <span>10</span>
      </div>
    </div>
  </div>
</template>

<script> import Vue from 'vue';
import { Toast } from 'vant';

Vue.use(Toast);
export default {
  name: 'Input',

评论

暂无评论