javascript:获取textarea光标和文字位置

2010年3月17日 | 分类: 技术 | 标签: 2,673浏览 | By admin

这是篇非常不错的关于如何获取textarea光标位置和获取textarea中选中的文字字符位置的文章,在这里转载一下,是方便日后有需要的时候能很方便的查看,虽然作者也是根据国外的一篇文章改写的,但写得还是非常不错的,另外这种写法不仅支持IE浏览器,也支持Firefox,以下是转载内容:

1、javascript部分

var start=0;
var end=0;
function add(){
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;
}
//下面是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;
}

2、调用部分:

<form action=”#”>
<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>
</form>

原文参见:http://blog.csdn.net/liujin4049/archive/2006/09/19/1244065.aspx

如无转载说明,则均为本站原创文章,转载请注明:来源:子猴博客





相关内容

  1. 太阳博客
    2010年3月18日09:27

    坐沙发,每次来都得登记。

  2. 2010年3月18日14:00

    打开你的网站,速度好慢啊

  3. 子猴
    2010年3月18日14:09

    妮裳神话 :

    打开你的网站,速度好慢啊

    有时我也感觉有些慢,不知道是不是因为访问量突然增加的原因,因为网站用的只是一个虚拟主机,而且是国外的,最近一段时间访问量比以前相比大了很多,不知道这个虚拟主机支持的程度如何

  4. 2010年3月18日16:50

    学习了!

*

酷!左边勾选上复选框,评论里将显示你博客文章!
:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!:

Upload Files

你可以上传一张或多张图片,这些图片将附在你评论里