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

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

setSelectionRange(selectionStart, selectionEnd)

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

 selectionStart = 0     ,   selectionEnd = 0

 

 selectionStart = 3     ,   selectionEnd = 3

 

 selectionStart = 3     ,   selectionEnd = 7

 

 selectionStart = 0     ,   selectionEnd = 11 

        要明白, selectionStart<=selectionEnd ,两个值相等时,就是一个光标,后者大于前者时,就是选中部分内容,当我们偏要把selectionStart的值设的大于selectionEnd时,运行时会自动将selectionStart的值赋值给selectionEnd,使之相等。

案例1

<template>
    <div>
      <!-- autofocus="autofocus"  加入该属性会自动获取焦点-->
      <input  id="edit" type="text" value='零一二三四五六七八九十' >
      <button @click="getSelect(1,5)">选择指定区域</button>
    </div>
</template>
<script>
  export default {
    data() {
      return {};
    },
    methods: {
      getSelect(res1,res2){
        const editValue = document.getElementById("edit");
          editValue.focus();  //控制光标,首先要获取到焦点
          edit.setSelectionRange(res1,res2);
          console.log(edit.selectionStart);
          console.log(edit.selectionEnd);
      },
    }
  }
</script>
<style scoped>
    div{
      height: 40px;
      width:260px;
    }
    input{
      margin:10px 10px 10px 10px ;
    }
</style>

 结果:

 点击 选择指定区域时,会自动选择 “一二三四”  ,selectionStart = 1 , selectionEnd = 5 


        当然 也可以直接修改  selectionStart, selectionEnd 的值,不用setSelectionRange()方法。

      getSelect(res1,res2){
        const editValue = document.getElementById("edit");
          editValue.focus();  //控制光标,首先要获取到焦点
          edit.selectionStart = res1
          edit.selectionEnd = res2
          // edit.setSelectionRange(res1,res2);
          console.log(edit.selectionStart);
          console.log(edit.selectionEnd);
      },
    }

效果和原来是一样的。

案例2

先看效果:

 代码:

 

<template>
    <div>
      <!-- autofocus="autofocus"  加入该属性会自动获取焦点-->
      <input ref="inputTitle" id="edit" type="text" value='零一二三四五六七八九十' >
      <button @click="getedit(-1)">光标前移</button>
      <button @click="getedit(1)">光标后移</button>
      <button @click="selectAll()">{{isSelectAll}}</button>
    </div>
</template>
<script>
  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 = '全选文本'
        }
      }
    }
  }
</script>
<style scoped>
    div{
      height: 40px;
      width:260px;
    }
    input{
      margin:10px 10px 10px 10px ;
    }
</style>

### 在 UniApp 中设置或更改输入框组件的光标位置 在 UniApp 开发环境中,对于 `input` 组件而言,可以通过调用特定的方法来控制光标的起始位置以及结束位置。具体来说,可以利用 `setSelectionRange(start, end)` 方法实现这一功能[^3]。 此方法允许开发者指定光标位置或是选中文本区域: - 参数 `start` 定义了选择范围起点或者是放置光标的索引位置; - 参数 `end` 则用于设定终点;如果希望仅移动光标而不做任何选取,则可令 start 和 end 的值相同。 下面是一个简单的例子展示如何操作: ```html <template> <view> <!-- 输入框 --> <input type="text" v-model="value" ref="myInput"/> <!-- 设置光标按钮 --> <button @click="moveCursor">点击这里将光标移到第5个字符处</button> </view> </template> <script> export default { data() { return { value: '这是一个测试字符串' }; }, methods: { moveCursor() { this.$nextTick(() => { const inputComponent = this.$refs.myInput; if (inputComponent && inputComponent.setSelectionRange) { // 将光标定位到第五个字符后面(即index=4),此处可以根据需求修改 inputComponent.setSelectionRange(4, 4); } }); } } }; </script> ``` 上述代码片段展示了通过绑定至按钮点击事件的方式,在用户触发该事件之后,程序会尝试获取当前页面内的 `<input>` 元素实例并调用其上的 `setSelectionRange()` 函数完成光标位置调整的任务。 值得注意的是,由于不同平台可能存在差异,因此建议先确认目标平台上是否支持此类 API 调用,并考虑提供相应的兼容处理方案以确保应用能在尽可能多的情况下正常工作。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天命爱心职责~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值