Bootstrap

vue在鼠标光标指定位置插入文字

1、html

<el-button type="primary"  @click="addScriptParameters(item.name)">{{ item.name }}</el-button>

2、js

const addScriptParameters = (name) => {

  if (name) {

    const text = parameter.value.$refs.textarea

    if (document.selection) {

      const sel = document.selection.createRange()

      sel.text = `{${name}}`

    } else if (typeof text.selectionStart === 'number' && typeof text.selectionEnd === 'number') {

      const startPos = text.selectionStart

      const endPos = text.selectionEnd

      let cursorPos = startPos

      const tmpStr = dataShow.value.patter

      cursorPos += name.length

      dataShow.value.patter = tmpStr.substring(0, startPos) + `{${name}}` + tmpStr.substring(endPos, tmpStr.length)

      text.selectionStart = text.selectionEnd = cursorPos

    } else {

      dataShow.value.patter += `{${name}}`

    }

    parameter.value.focus()

  }

}

 

;