html>
<head>
<script>
function storecaret (textel) {
if (textel.createtextrange)
textel.caretpos = document.selection.createrange().duplicate();
}
function insertatcaret (textel, text) {
if (textel.createtextrange && textel.caretpos) {
var caretpos = textel.caretpos;
caretpos.text =caretpos.text.charat(caretpos.text.length - 1) == ' ' ?text + ' ' : text;
}
else
textel.value = text;
}
</script>
</head>
<body>
<form name="aform">
<textarea name="atextarea" rows="5" cols="80" wrap="soft" onselect="storecaret(this);" onclick="storecaret(this);" onkeyup="storecaret(this);"> 这是例子,你可以在这里添加文字、插入文字。 </textarea>
<br>
<input type="text" name="atext" size="80" value="我要在光标处插入这些文字"><br>
<input type="button" value="我要在光标处插入上面文本框里输入的文字!" onclick="insertatcaret(this.form.atextarea, this.form.atext.value);">
</form>
</body>
</html>
闽公网安备 35060202000074号