感觉老师这里的代码有一点点小问题

来源:1-11 指定光标位置插入自定义内容格式

_zion_

2020-11-19 09:12:42

# 具体遇到的问题
 // 计算光标的当前位置

    getPos () {

      let cursorPos = 0

      let elem = document.getElementById('edit')

      if (document.selection) {

        // IE

        let selectRange = document.selection.createRange()

        selectRange.moveStart('character'-elem.value.length)

        cursorPos = selectRange.text.length

      } else if (elem.selectionStart || elem.selectionStart === '0') {

        cursorPos = elem.selectionStart

      }

      this.pos = cursorPos

    }


else if 的判断有问题,elem.selectionStart === '0'改为elem.selectionStart === 0,或者整个判断改为

typeof elem.selectionStart  === ‘number’。

selectionStart属性的值是数字,原来的判断方式,当selectionStart===0时不会执行cursorPos=elem.selectionStart

——————————————

不过改不改不影响最后的结果,因为cursorPos有个初始值是0


在这里输入代码,可通过选择【代码语言】突出显示

写回答

1回答

Brian

2020-11-21

?​好的,提出问题,虚心接受~~~

1

0 学习 · 1842 问题

查看课程