将文字插入到光标处!!
来源:互联网 发布:天庭淘宝城 编辑:程序博客网 时间:2024/06/05 10:05
<script type="text/javascript">
var start=0;
var end=0;
function add(){
alert(start+"/n"+end);
var textBox = document.getElementById("ta");
var pre = textBox.value.substr(0, start);
var post = textBox.value.substr(end);
textBox.value = pre + document.getElementById("inputtext").value + post;
}
function savePos(textBox){
//如果是Firefox(1.5)的话,方法很简单
if(typeof(textBox.selectionStart) == "number"){
start = textBox.selectionStart;
end = textBox.selectionEnd;
end = textBox.selectionEnd;
}
//下面是IE(6.0)的方法,麻烦得很,还要计算上'/n'
else if(document.selection){
var range = document.selection.createRange();
if(range.parentElement().id == textBox.id){
// create a selection of the whole textarea
var range_all = document.body.createTextRange();
range_all.moveToElementText(textBox);
//两个range,一个是已经选择的text(range),一个是整个textarea(range_all)
//range_all.compareEndPoints() 比较两个端点,如果range_all比range更往左(further to the left),则 //返回小于0的值,则range_all往右移一点,直到两个range的start相同。
// calculate selection start point by moving beginning of range_all to beginning of range
for (start=0; range_all.compareEndPoints("StartToStart", range) < 0; start++)
range_all.moveStart('character', 1);
// get number of line breaks from textarea start to selection start and add them to start
// 计算一下/n
for (var i = 0; i <= start; i ++){
if (textBox.value.charAt(i) == '/n')
start++;
}
// create a selection of the whole textarea
var range_all = document.body.createTextRange();
range_all.moveToElementText(textBox);
// calculate selection end point by moving beginning of range_all to end of range
for (end = 0; range_all.compareEndPoints('StartToEnd', range) < 0; end ++)
range_all.moveStart('character', 1);
// get number of line breaks from textarea start to selection end and add them to end
for (var i = 0; i <= end; i ++){
if (textBox.value.charAt(i) == '/n')
end ++;
}
}
}
document.getElementById("start").value = start;
document.getElementById("end").value = end;
}
</script>
<body>
<table border="1" cellspacing="0" cellpadding="0">
<tr>
<td>start: <input type="text" id="start" size="3"/></td>
<td>end: <input type="text" id="end" size="3"/></td>
</tr>
<tr>
<td colspan="2">
<textarea id="ta" onkeydown="savePos(this)"
onkeyup="savePos(this)"
onmousedown="savePos(this)"
onmouseup="savePos(this)"
onfocus="savePos(this)"
rows="14" cols="50"></textarea>
</td>
</tr>
<tr>
<td><input type="text" id="inputtext" /></td>
<td><input type="button" onClick="add()" value="Add Text"/></td>
</tr>
</table>
</body>
- 将文字插入到光标处!!
- JS往textarea的光标所在处添加文字+将光标移动到文字末尾
- 关于NTKO将代码插入到光标处
- TextView在光标处插入文字
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- JS在TextArea光标位置插入文字+移动光标到文字末尾
- 【记录】JS在TextArea光标位置插入文字+移动光标到文字末尾
- C# 在文档光标处插入文字后光标移至被插入文字后
- EditText将光标移动到文字的结尾
- EditText将光标挪动到文字的结尾
- js 将光标移动到textarea的最后和光标位置插入内容
- 在Outlook新邮件中向光标所在处插入一段新文字然后光标后移
- 将文字内容和图片插入到 Access 数据库
- 将文字内容和图片插入到 Access 数据库
- 将文字内容和图片插入到 Access 数据库
- 将文字内容和图片插入到 Access 数据库
- 将input中的光标移动到文字的末尾后,怎么用js显示光标当前的位置?
- 通过cmd命令安装、卸载、启动和停止Windows Service(InstallUtil.exe)
- 由安全沙箱冲突:LoaderInfo.content。
- 2010年的总结
- 就差一位就可以完成任务,就好下载资料了,麻烦加个好友。
- 从Service中启动Acitivity时向Activity中传递参数
- 将文字插入到光标处!!
- windows平台下vlc编译
- Winsock;TCP/IP编程;模板代码;VC++;单线程;
- Linux下各类TCP网络服务器的实现源代码
- AWS主机安装中文
- BCGControlBar的使用方法
- Word/Excel/PDF文件转换成HTML整理
- 57条网站SEO高手的技巧
- eBay Architecture(5)–Partition Everything[Database]