给你个例子参考一下(摘自以前的帖子):
<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="<img>"><BR>
<INPUT TYPE="button" VALUE="我要在光标处插入上面文本框里输入的文字!" ONCLICK="insertAtCaret(this.form.aTextArea, this.form.aText.value);">
</FORM>
</BODY>
</HTML>
<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="<img>"><BR>
<INPUT TYPE="button" VALUE="我要在光标处插入上面文本框里输入的文字!" ONCLICK="insertAtCaret(this.form.aTextArea, this.form.aText.value);">
</FORM>
</BODY>
</HTML>
解决方案 »
- 拜求此下拉菜单导航
- 使用javascript 放大、缩小图片
- 请教一个网络树形结构生成问题??
- 如何实现成批上传图片,不用<input type=file>控件
- 谁知到哪里能下载javascript和AJAX方面的API,不要Jscript
- ●●古怪的javascript问题,几个图片放在不同的<div id=111></div>中,如何根据图片名得到其div的ID?
- 如何做FLASH浮动广告?(适用于各种浏览器)
- 急求高手解决
- 用div做输入框,如何限制输入的纯文本字数
- jquery mobile做的滑动和点击效果为何在华为手机上失效
- 微软主页那样的菜单,是怎么做的?100分奉上,在线等……
- 请教一个按钮问题。
function test(){
var str="<br><input><input type='checkbox'><input type='radio' name=a><input type='radio' name=a><input type='radio' name=a>"
document.all.mxh.insertAdjacentHTML("afterBegin",str)
}
</script>
<div id=mxh></div>
<input type=button value=insert onclick="test()">
createTextRange方法不能用于div。to:grooving(峡谷)
insertAdjacentHTML只有beforeBegin,afterBegin,beforeEnd,afterEnd四个参数,还不能实现在中间插入啊麻烦大家再指点一下了,