我想创建一个简单的函数,将文本添加到用户光标位置的文本区域中。它需要是一个干净的函数。这只是最基本的。剩下的我会弄明白的。
发布于 2013-11-12 10:42:40
将其更改为getElementById(myField)
function insertAtCursor(myField, myValue) {
// IE support
if (document.selection) {
document.getElementById(myField).focus();
sel = document.selection.createRange();
sel.text = myValue;
}
// MOZILLA and others
else if (document.getElementById(myField).selectionStart || document.getElementById(myField).selectionStart == '0') {
var startPos = document.getElementById(myField).selectionStart;
var endPos = document.getElementById(myField).selectionEnd;
document.getElementById(myField).value =
document.getElementById(myField).value.substring(0, startPos)
+ myValue
+ document.getElementById(myField).value.substring(endPos, document.getElementById(myField).value.length);
} else {
document.getElementById(myField).value += myValue;
}
}https://stackoverflow.com/questions/11076975
复制相似问题