<!-- lang: html -->
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head> <meta charset="utf-8" /> <title></title> <script> // 在光标位置插入 function insertText(obj, str) { // IE Support if (document.selection) { var sel = document.selection.createRange(); sel.text = str;
}
// Firefox Support
else if (typeof (obj.selectionStart) == 'number' && typeof( obj.selectionEnd) == 'number') {
var startPos = obj.selectionStart,
endPos = obj.selectionEnd,
cursorPos = startPos,
tmpStr = obj.value;
obj.value = tmpStr.substring(0, startPos) + str + tmpStr.substring(endPos, tmpStr.length);
cursorPos += str.length;
obj.selectionStart = obj.selectionEnd = cursorPos;
} else {
obj.value += str;
}
}
// 移动到内容的尾部
function moveEnd(obj) {
obj.focus();
var len = obj.value.length;
if (document.selection) {
var sel = obj.createTextRange();
sel.moveStart('character', 0);
sel.collapse(true);
sel.select();
}
else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
obj.selectionStart = obj.selectionEnd = len;
}
}
</script>
<style type="text/css">
#text {
width: 615px;
}
</style>
</head> <body>
<input type="button" onclick="insertText(document.getElementById('text'), 'YoyiorLee')" value="插入文字" />
<input type="button" onclick="moveEnd(document.getElementById('text'))" value="移到末尾" />
<br />
<textarea id="text" rows="3">
</textarea>
</body> </html>