首页 > 其他分享 >contentEditable实现div可编辑,控制插入节点(兼容IE)

contentEditable实现div可编辑,控制插入节点(兼容IE)

时间:2023-12-25 10:36:45浏览次数:27  
标签:contentEditable setAttribute inp value range input div IE 节点

实现可文字编辑也可插入节点的功能

展示如下:

contentEditable实现div可编辑,控制插入节点(兼容IE)_html

html5中contentEditable属性规定是否可编辑元素的内容,给需要编辑的节点添加contentEditable="true"。 兼容性:

contentEditable实现div可编辑,控制插入节点(兼容IE)_chrome_02

当点击Button时在编辑框内增加节点:

contentEditable实现div可编辑,控制插入节点(兼容IE)_chrome_03

一开始div中加的span标签,发现有几个缺陷:

  1. 点删除键时span不会删除整个,而是一个一个删除span里的字符
  2. 当有连续的span时,光标会超出容器宽度在容器外面闪烁

针对以上第一个问题试了以下两种方法:

  1. <span contentEditable="false"></span>
  2. 设置css user-modify:设置css user-modify: read-only

两种方法都能解决无法删除整个节点的问题,但光标超出容器依然在╮( ̄▽  ̄)╭各种翻找资料后找到一个解决办法: 加input节点,并且设置disabled

var inp = document.createElement("input");
inp.setAttribute("value", value);
inp.setAttribute("data-code", code);
inp.setAttribute("disabled", 'true');
inp.style.width = this.input_resize(value) + 'px';
range.insertNode(inp);

input是不会自适应文本内容的,于是就有了input_resize去计算宽度,

input_resize(html) {
        inputResizeDom.innerHTML = html;
        inputResizeDom.style.fontSize = '16px';
        return inputResizeDom.getBoundingClientRect().width.toFixed(4);
    }

方法是给另一个节点填充相同的内容,然后取这个节点的宽度作为input节点的宽度,需要注意的是,计算宽度的节点和 input的font-sizefont-family是相同的。

说到这个,不知道大家有木有注意到range.insertNode(inp)这么一句,他的作用是在存储的光标位置处添加节点,Range对象API。Range对象初始化:

//ie 9以下
var docSelection = document.selection;
//chrome opera safari兼容
var winSelection = window.getSelection;
handleBlur() {
    if(!docSelection && winSelection){
        sel = window.getSelection();
        hasRange = sel.getRangeAt && sel.rangeCount;
        if(hasRange){
            range = sel.getRangeAt(0);
            range.deleteContents();
        }
    }
}
handleKeyUp() {
    //ie获取光标定位
    if(docSelection){
        range = document.selection.createRange();
    }
},

为什么在ie浏览器下要在keyup触发方法中重置range,这就是有些坑了,chrome中blur事件触发时range记录光标在容器中的位置,但是在IE中记录的是blur时点击的元素(就是容器以外的元素),于是我在keyup事件中对range做一个存储。顺便说一下,ie对onInput事件兼容性不是很好,慎重使用昂~。 点击按钮时触发操作:

selectLabel(value,code,event){
    var target = event.currentTarget;
     if(!$(target).hasClass('disabled')){
         if(docSelection && document.selection.type != "Control"){
             // IE9以下
             let html = `<input value=${value} data-code=${code} disabled="true" style="width: ${this.input_resize(value) + 'px'}"/>`
             range.pasteHTML(html);
         }else if(winSelection && hasRange){
             var inp = document.createElement("input");
             inp.setAttribute("value", value);
             inp.setAttribute("data-code", code);
             inp.setAttribute("disabled", 'true');
             inp.style.width = this.input_resize(value) + 'px';
             range.insertNode(inp);
         }
         $(target).addClass('disabled')
         this.setFocus($('#contentSms'))
     }
}

几个用到的资料:contentEditable API光标的使用

标签:contentEditable,setAttribute,inp,value,range,input,div,IE,节点
From: https://blog.51cto.com/u_16384993/8963823

相关文章

  • EKP qhky 流程启接口addReview 支持明细表地址本传递多值
     问题描述: 流程启动接口(addReview),给第一行多人员地址本字段 传多个部门,后台代码解析不成功     /** * *20230703定制处理附件列表问题sjx * ***/ for(inti=0;i<attForms.size();i++){ S......
  • 汇编-idiv有符号整数除法
     有符号除法就是将一个有符号数除以另一个有符号数有符号整数除法与无符号除法几乎相同,只有一个重要的区别:在进行除法之前,必须将被除数进行符号扩展。为了说明为何有此必要,我们先不这么做。下面的代码使用MOV将-101赋值给AX,即DX:AX的低半部分:       ......
  • Vue3之实现一个可拖拽的div
    实现一个可拖拽的div写法如下:constchatbox=ref();constdragx=(el)=>{letoDiv=chatbox.value;//当前元素letdisX=el.clientX-oDiv.offsetLeft;letdisY=el.clientY-oDiv.offsetTop;document.onmousemove=function(e){//通过事件委托,......
  • 汇编-div无符号整数除法
      在32位模式下,DIV(无符号整数除法)指令执行8位、16位及32位的无符号整数除法。无符号除法(unsigneddivision)定义为一个无符号数除以另一个无符号数。其中,除数为单个寄存器或内存操作数。格式如下: 【a=c÷b,读作c除以b(或b除c)。其中,c叫做被除数,b叫做除数】 下表给......
  • codeforces刷题(1100):1907C_div3
    C、RemovalofUnattractivePairs跳转原题点击此:该题地址1、题目大意  给定一个字符串,可以删除相邻的两个不相等的字符。问你删除后能得到最小的字符串长度为多少。2、题目解析  因为只要两个不相等的字符相邻就能消除,所以只需要找到数量最多的字符,只要它的数量比其它字......
  • Taylor series Explicit Euler Implicit Euler
    1Taylorseries\[\begin{gathered}\sum_{n=0}^{\infty}\frac{f^{(n)}\left(a\right)}{n!}\left(x-a\right)^{n}\\f\left(a\right)+\frac{f^{\prime}\left(a\right)}{1!}(x-a)+\frac{f^{\prime\prime}\left(a\right)}{2!}(x-a)^{2}+\frac{f^{\prime......
  • Remove TraceParent header from HttpClient requests
    ASP.NETCorecreatesanActivitythatrepresentstherequestbydefault.ThisiswhattellsHttpClienttosendanoutgoingrequestidheader.Youcandisableitinacoupleofways:Bysettingthecurrentactivitytonullbeforemakingtherequest(Activi......
  • Pandas数据分析实战(Pandas in action)第3章 Series 方法
    Pandas数据分析实战第3章Series方法read_csv()导入数据集pd.read_csv(filepath_or_buffer="./file/chapter_03/pokemon.csv")#或者pd.read_csv("./file/chapter_03/pokemon.csv")PokemonType0BulbasaurGrass/Poison1......
  • 【阅读笔记】图像增强-《Efficientcontrast enhancement using adaptive gamma correc
    2013年发表在TIP上的对比度增强算法AGCWD(Efficientcontrastenhancementusingadaptivegammacorrectionwithweightingdistribution)提出了一种自动映射技术,通过亮度像素的伽马校正和概率分布来提高调暗图像的亮度。为了增强视频,所提出的图像增强方法使用关于每帧之间差异的时......
  • @ConfigurationProperties(prefix = “xx.xx.xx“) 从配置文件中取值赋给类的属性
    @ConfigurationProperties(prefix=“xx.xx.xx“)从配置文件中取值赋给类的属性@ConfigurationProperties(prefix=“xx.xx.xx”)该注解的作用是从配置文件中取值赋给类的属性,当然也可以为方法的变量赋值/***服务访问URL*/@Component@ConfigurationProperties(value......