input?獲取光標(biāo)位置設(shè)置光標(biāo)位置方案
需求
輸入框,支持鍵盤輸入與快捷按鍵 輸入,UI 如下「基于antd」:

關(guān)鍵點(diǎn):鍵盤輸入直接使用 input onChange 事件即可,快捷按鍵輸入需要根據(jù)光標(biāo)位置插入,插入后光標(biāo)在新插入的字符后。
解決方案
獲取 input 光標(biāo)位置
通過 element.selectionStart 獲取光標(biāo)位置。
const inputDom = document.getElementById("input")
const selectionStart = inputDom.selectionStart
如圖,此時(shí)的 selectionStart 是 3。
設(shè)置 input 光標(biāo)
通過 element.setSelectionRange() 設(shè)置光標(biāo)位置,前提是 input 被 focus。
inputDom.focus()
// focus() 異步,所以加了 setTimeout
setTimeout(() => {
const nextSelection = selectionStart + 1
inputDom.setSelectionRange(nextSelection, nextSelection)
}, 0)element.setSelectionRange 語法
element.setSelectionRange(selectionStart, selectionEnd [, selectionDirection]);
- selectionStart: 被選中的第一個(gè)字符的位置索引, 從 0 開始。如果這個(gè)值比元素的 value 長度還大,則會(huì)被看作 value 最后一個(gè)位置的索引。
- selectionEnd: 被選中的最后一個(gè)字符的下一個(gè)位置索引。如果這個(gè)值比元素的 value 長度還大,則會(huì)被看作 value 最后一個(gè)位置的索引。
- selectionDirection:選擇方向。forward/backward/none
如果 selectionStart 與 selectionEnd 相同,不選中任何,光標(biāo)聚集在 selectionStart/selectionEnd。
如果 selectionEnd 小于 selectionStart,不選中任何,光標(biāo)聚集在在 selectionEnd。
inputDom.setSelectionRange(0, 4)表現(xiàn)如下圖:

完整代碼
import React, { useState, useRef } from 'react'
import { throttle } from 'lodash'
import { Input, Button, Tag } from 'antd'
const SHORTCUT = [0, '*', '#', 9]
const InputPro = () => {
const [value, setValue] = useState('')
const inputRef = useRef()
const handleSubmit = throttle(() => {
console.log('value', value)
}, 3000)
const handleInputChange = e => {
setValue(e.target.value?.trim())
}
const handleTagChange = val => {
const inputDom = inputRef.current?.input || {}
let selectionStart = inputDom.selectionStart
if (typeof selectionStart !== 'number') {
selectionStart = value.length
}
const data = `${value.slice(
0,
selectionStart,
)}${val}${value.slice(selectionStart)}`
if (data.length <= 25) {
setValue(data)
inputDom.focus()
setTimeout(() => {
const nextSelection = selectionStart + 1
inputDom.setSelectionRange(nextSelection, nextSelection)
}, 0)
}
}
return (
<div>
<Input.Group compact>
<Input
allowClear
style={{ width: 160 }}
maxLength={25}
placeholder='請(qǐng)輸入'
value={value}
onChange={handleInputChange}
ref={inputRef}
/>
<Button
type='primary'
onClick={handleSubmit}
disabled={value.length === 0}
>
確認(rèn)
</Button>
</Input.Group>
<div style={{ display: 'flex', alignItems: 'center', marginTop: 8 }}>
{SHORTCUT.map(itm => (
<div key={itm} onClick={() => handleTagChange(itm)}>
<Tag color="#108ee9">{itm}</Tag>
</div>
))}
</div>
</div>
)
}
export default InputPro以上就是input 獲取光標(biāo)位置設(shè)置光標(biāo)位置方案的詳細(xì)內(nèi)容,更多關(guān)于input 獲取設(shè)置光標(biāo)位置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JS函數(shù)進(jìn)階之prototy用法實(shí)例分析
這篇文章主要介紹了JS函數(shù)進(jìn)階之prototy用法,結(jié)合實(shí)例形式分析了JavaScript函數(shù)中使用prototy擴(kuò)展屬性相關(guān)操作技巧,需要的朋友可以參考下2020-01-01
js中查找最近的共有祖先元素的實(shí)現(xiàn)代碼
司徒正美給出的題,也嘗試著寫一下。希望大家多多交流。2010-12-12
基于JS實(shí)現(xiàn)前端壓縮上傳圖片的實(shí)例代碼
這篇文章主要介紹了基于JS實(shí)現(xiàn)前端壓縮上傳圖片的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
JavaScript Date對(duì)象應(yīng)用實(shí)例分享
JS遍歷樹層級(jí)關(guān)系實(shí)現(xiàn)原理解析

