<html>
<head>
<script type='text/javascript'>
function test(str){ var tc = document.getElementById("mytextarea");
var tclen = tc.value.length;
tc.focus();
if(typeof document.selection != "undefined")
{
document.selection.createRange().text = str;
}
else
{
tc.value = tc.value.substr(0,tc.selectionStart)+str+tc.value.substring(tc.selectionStart,tclen);
}
}
</script>
</head>
<body>
<textarea rows=5 name=s1 cols=27 id="mytextarea">目的通过点击页面上的按钮button 在textarea中的光标停留处插上文字 </textarea>
<input type=button onclick="test('这是需要加入的文字')" value="追加" />
</body>
</html>
解决方案 »
- js实现在线搜索+手写功能特效
- js读取串口数据
- My97datepicker 引发document.body为空或不是对象
- 如何将google api 搜索出来的内容输出到html页面
- 是Bug还是我写的有问题?
- addClass加载类,加载的类在创建时为什么一定要写成 #(id名).(class类名); 而直接写 .(class类名)就不能被调用
- 菜问题,关于vbs脚本
- 如何知道所选择的文本是否是粗体
- 请问如何实现隐藏IE窗体中的关闭、最大话按钮?不激活也可以!
- 怎么实现新开小窗口自动最小化功能?在线等待!多谢各位大侠!!
- htmlarea 与onload事件
- 谁有带复选框的树形菜单,其中还要能连接数据库的,分全给了
document.getElementById("xx").value=sel.text
function insertAtCaret(text)
{
if(onFocusTextArea==null)
{
alert('请选择要输入的文本框!');
return;
}
if (onFocusTextArea.createTextRange && onFocusTextArea.caretPos)
{
var caretPos = onFocusTextArea.caretPos;
caretPos.text =caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;
}
else
onFocusTextArea.value = text;
}
答案原理如下<textarea name="Content" id="Content" cols="50" rows="5" class="iptxt" onSelect="javascript: storeCaret(this);" onClick="javascript: storeCaret(this);" onKeyUp="javascript: storeCaret(this);" AUTOCOMPLETE="off">function InsertTag(ControlID, Text)
{
var objElement;
objElement = document.getElementById(ControlID);
if (objElement.createTextRange && objElement.caretPos)
{
var caretPos1 = objElement.caretPos;
caretPos1.text = Text;
objElement.focus();
}
else
{
objElement.value += Text;
objElement.focus();
}
return false;
}
function storeCaret (textEl){ if(textEl.createTextRange){
textEl.caretPos = document.selection.createRange().duplicate();
}
}