急用,在线等:往表单域中插入内容?

aspmaster 2003-06-19 12:01:37
想往<textarea>域里当间光标所在位置插入一段内容,请问该如何实现?
代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<script language="JavaScript">
function insert()
{
document.form1.content.value="这是要插入的内容";
//这里只是将那个content中的所有内容变成"这是要插入的内容",如何实现content中的内容变成"这是原有的内容,这是要插入的内容,将新内容插入到当前光标的位置!"
}
</script>
</head>
<body>
<form name="form1" method="post" action="">
<textarea name="content" cols="50" rows="6" id="content">这是原有的内容,,将新内容插入到当前光标的位置!</textarea>
<br>
<input type="button" name="Submit" value="插入" onClick="insert()">
</form>
</body>
</html>

这个代码中,没有实现那个功能,如何实现????
...全文
62 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
aspmaster 2003-06-19
  • 打赏
  • 举报
回复
太感谢了,过分。!
希默软件 2003-06-19
  • 打赏
  • 举报
回复
<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;
}
function deleteAtCaret (textEl) {
if (textEl.createTextRange && textEl.caretPos) {
var caretPos = textEl.caretPos;
document.selection.clear();
}
//alert(aForm.aTextArea.
}
</script>
<form name=aForm>
<input name=aTextArea size=120 wrap=soft onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);" value=这是例子,你可以在这里添加文字、插入文字。>
<br>
<input type=text name=aText size=80 value=我要在光标处插入这些文字><br>
<input type=button value=我要在光标处插入上面文本框里输入的文字! onclick="insertAtCaret(this.form.aTextArea, this.form.aText.value);">
<input type=button value=删除选中文字 onclick=deleteAtCaret(this.form.aTextArea); id=button1 name=button1>
</FORM>

87,988

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧