-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
103 lines (93 loc) · 3.01 KB
/
test.html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>haiyang.me > input输入框获取光标位置,在光标处插入文字,替换选中文字</title>
<script>
//获取光标位置函数
// function getCursorPosition(elem) {
// var pp = 0
// // IE
// if (document.selection) {
// elem.focus()
// var aa = document.selection.createRange()
// aa.moveStart('character', -elem.value.length)
// pp = aa.text.length
// }
// // FF, Chrome
// else if (elem.selectionStart || elem.selectionStart == '0') {
// pp = elem.selectionStart
// }
// return pp
// }
// //设置光标位置函数
// function setCaretPosition(elem, pos) {
// // IE
// if (elem.setSelectionRange) {
// elem.focus()
// elem.setSelectionRange(pos, pos)
// }
// // FF, Chrome
// else if (elem.createTextRange) {
// var range = elem.createTextRange()
// range.collapse(true)
// range.moveEnd('character', pos)
// range.moveStart('character', pos)
// range.select()
// }
// }
function insertAtCursor(elem, value) {
debugger
var field = elem
var newValue = ''
// IE support
if (document.selection) {
field.focus()
var sel = document.selection.createRange()
sel.text = newValue = value
sel.select()
} else if (field.selectionStart || field.selectionStart === 0) {
var startPos = field.selectionStart
var endPos = field.selectionEnd
var restoreTop = field.scrollTop
newValue = field.value.substring(0, startPos) +
value +
field.value.substring(endPos, field.value.length)
if (restoreTop > 0) {
field.scrollTop = restoreTop
}
field.value = newValue
field.focus()
setTimeout(function () {
field.selectionStart = startPos + value.length
field.selectionEnd = startPos + value.length
}, 0)
} else {
newValue = field.value + value
field.value = newValue
field.focus()
}
}
</script>
</head>
<body>
<input id="a" type=text name=text1 value="1234567890">
<input id="b" type=text name=text1 value="1234567890">
<button onclick="alert(getCursorPosition(a))">获取光标位置</bottun>
<button onclick="setCaretPosition(a, 5)">设置光标位置: 5</bottun>
<button onclick="insertAtCursor(a, 'ww')">替换或插入文字: ww</bottun>
</body>
<script>
'undefined' === typeof _trfq || (window._trfq = []);
'undefined' === typeof _trfd && (window._trfd = []), _trfd.push({
'tccl.baseHost': 'secureserver.net'
}), _trfd.push({
'ap': 'cpsh-oh'
}, {
'server': 'sg2plzcpnl458828'
}, {
'id': '7677590'
}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.
</script>
<script src='https://img1.wsimg.com/tcc/tcc_l.combined.1.0.6.min.js'></script>
</html>