avatar

前端/控制光标

网上的例子,借鉴后修改为react版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<form id="form1" name="form1" method="post" action="">
<label>
<textarea name="text" id="text" cols="45" rows="10">
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
这是测试内容,请在任意位置插入内容。
</textarea>

<a id="insert" href="javascript:void(0);">{PHP code here}</a>
</label>
</form>
<script type="text/javascript">
var text = document.getElementById('text');
var insert = document.getElementById('insert');
insert.onclick = function () {
insertAtCursor(text, this.innerHTML);
};

function insertAtCursor(myField, myValue) {
if (document.selection) {
//IE support
myField.focus();
var sel = document.selection.createRange();
sel.text = myValue;
sel.select();
} else if (myField.selectionStart || myField.selectionStart == '0') {
//MOZILLA/NETSCAPE support
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
var beforeValue = myField.value.substring(0, startPos);
var afterValue = myField.value.substring(endPos, myField.value.length);

myField.value = beforeValue + myValue + afterValue;

myField.selectionStart = startPos + myValue.length;
myField.selectionEnd = startPos + myValue.length;
myField.focus();
} else {
myField.value += myValue;
myField.focus();
}
}
</script>

我自己的例子 react 版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

const numberKeyPress = (key) => {
let inputDom = document.getElementById('input');
insertAtCursor(inputDom, key);
};
const insertAtCursor = (myField, myValue) => {
if (document.selection) {
//IE support
myField.focus();
let sel = document.selection.createRange();
sel.text = myValue;
sel.select();
} else if (myField.selectionStart || myField.selectionStart == '0') {
//MOZILLA/NETSCAPE support
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
var beforeValue = myField.value.substring(0, startPos);
var afterValue = myField.value.substring(endPos, myField.value.length);

myField.value = beforeValue + myValue + afterValue;
setInputValue(beforeValue + myValue + afterValue);
myField.selectionStart = startPos + myValue.toString().length;
myField.selectionEnd = startPos + myValue.toString().length;
myField.focus();
} else {
myField.value += myValue;
myField.focus();
}
};
const backspace = () => {
let inputDom = document.getElementById('input');

let selectionEnd = inputDom.selectionEnd;

if (selectionEnd === 0) {
return;
}
let arr = inputValue.split('');
arr.splice(selectionEnd - 1, 1);
let str = arr.join('');
setInputValue(str);

inputDom.focus();
//必须加延时
// 在浏览器默认行为focus之后执行, 实现非阻塞;
setTimeout(() => {
inputDom.selectionEnd = selectionEnd - 1;
}, 0);
};

<Input
prefix={<SearchOutlined />}
size="large"
placeholder={placeholder}
id="input"
value={inputValue}
onChange={(e) => {
setInputValue(e.target.value);
}}
/>
文章作者: 小黑
文章链接: http://yoursite.com/2023/01/17/前端/控制光标/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 小黑的小站
打赏
  • 微信
    微信
  • 支付寶
    支付寶
2