input输入框控制光标位置 setSelectionRange()的使用

您所在的位置:网站首页 修改input光标宽度 input输入框控制光标位置 setSelectionRange()的使用

input输入框控制光标位置 setSelectionRange()的使用

2023-12-11 16:19| 来源: 网络整理| 查看: 265

当我们点击编辑时,按照习惯,光标应该在字符串的结尾,在不对光标进行任何设置的情况下,光标是出现在开头的,所以要进行相关操作。

setSelectionRange(selectionStart, selectionEnd)

该方法是对光标进行操作的, selectionStart, selectionEnd 为控制光标位置的参数,我们用例子来了解:

 selectionStart = 0     ,   selectionEnd = 0

 

 selectionStart = 3     ,   selectionEnd = 3

 

 selectionStart = 3     ,   selectionEnd = 7

 

 selectionStart = 0     ,   selectionEnd = 11 

        要明白, selectionStart 光标前移 光标后移 {{isSelectAll}} export default { data() { return { isSelectAll:'全选文本' }; }, methods: { getedit(res){ const editTask = document.getElementById("edit"); editTask.focus(); edit.setSelectionRange(edit.selectionStart+res, edit.selectionEnd+res); }, //全选内容 selectAll(){ const editTask = document.getElementById("edit"); editTask.focus(); if(this.isSelectAll == '全选文本'){ editTask.selectionStart = 0 // this.$refs.inputTitle.value.length 为input框中文本长度 editTask.selectionEnd = this.$refs.inputTitle.value.length this.isSelectAll = '取消全选' }else{ editTask.selectionStart = 0 editTask.selectionEnd = 0 this.isSelectAll = '全选文本' } } } } div{ height: 40px; width:260px; } input{ margin:10px 10px 10px 10px ; }



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3