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='請輸入'
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)位置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Js數(shù)組扁平化實(shí)現(xiàn)方法代碼總匯
這篇文章主要介紹了Js數(shù)組扁平化實(shí)現(xiàn)方法代碼總匯,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
深入webpack打包原理及l(fā)oader和plugin的實(shí)現(xiàn)
這篇文章主要介紹了深入webpack打包原理及l(fā)oader和plugin的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
CountUp.js實(shí)現(xiàn)數(shù)字滾動(dòng)增值效果
這篇文章主要為大家詳細(xì)介紹了CountUp.js實(shí)現(xiàn)數(shù)字滾動(dòng)增值效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
微信小程序數(shù)據(jù)操作指南之從綁定到更新的操作方法
在微信小程序開發(fā)中,數(shù)據(jù)操作是不可或缺的一環(huán),文章詳細(xì)介紹了數(shù)據(jù)綁定、更新等方法,并提供示例和注意事項(xiàng),幫助開發(fā)者更好地應(yīng)用這些技術(shù),本文給大家介紹微信小程序數(shù)據(jù)操作指南之從綁定到更新,感興趣的朋友跟隨小編一起看看吧2024-10-10
JS實(shí)現(xiàn)利用閉包判斷Dom元素和滾動(dòng)條的方向示例
這篇文章主要介紹了JS實(shí)現(xiàn)利用閉包判斷Dom元素和滾動(dòng)條的方向,涉及javascript閉包、事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)使用技巧,需要的朋友可以參考下2019-08-08
javascript遍歷json對象的key和任意js對象屬性實(shí)例
下面小編就為大家?guī)硪黄猨avascript遍歷json對象的key和任意js對象屬性實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03

