新闻详情

在textarea文本域中插入文本

3863 2009/5/19
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>标题页</title>
<script LANGUAGE="JavaScript">
//选择文本时保存光标位置-单击时同样
function storePos (txtobj)
{
    if (txtobj.createTextRange)                      //获取选中的内容
        txtobj.caretPos = document.selection.createRange().duplicate();
}
function insertTextArea (txtobj, text)
{
    if (txtobj.createTextRange && txtobj.caretPos) {
       var caretPos = txtobj.caretPos;               //获取光标所在的位置
       //替换光标处位置
       caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) =='' ?text + '' : text;
       }
    else
       txtobj.value = text;                          //直接显示插入的文本
}
</script>
</head>
<body>
<TEXTAREA NAME="mytxt" ROWS="5" COLS="25" WRAP="soft" onSelect="storePos(this);" onClick="storePos(this);" onKeyUp="storePos(this);"> 实现文本的插入,文本框可以实现增、删、改功能</TEXTAREA>
<br />
<INPUT TYPE="text" NAME="insertTxt" SIZE="20" VALUE="要插入的文本"><br />
<INPUT TYPE="button" VALUE="插入文本" onClick="insertTextArea(mytxt, insertTxt.value);">
</body>
</html>

相关资讯

18973218026
返回顶部