用javascript獲取textarea中的光標(biāo)位置
更新時間:2008年05月06日 23:20:22 作者:
Javascript一向以他的靈活隨意而著稱,這也使得它的功能可以非常的強(qiáng)大,而由于沒有比較好的調(diào)試工具,又使得它使用起來困難重重,尤其使對于一些初學(xué)者,更是感覺到無從下手。今天探討的問題是用javascript獲取textarea中光標(biāo)的位置。
對于寫javascript寫網(wǎng)頁編輯器的人來說,獲取textarea中的光標(biāo)位置是一個非常重要的問題,而往往很多人在這個地方不知所措,找不到好的辦法。昨天我在網(wǎng)上找到了一段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,一個是已經(jīng)選擇的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;
}
下面是在頁面中調(diào)用js代碼的方法:
<form action="a.cgi">
<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,在此謝過!
這段js代碼同時支持IE和Firefox,甚是精彩,可見此人js功力深厚啊,呵呵。
Btw:聽說Firefox現(xiàn)在的市場占有率已經(jīng)達(dá)到17%了,而IE8也快出來了,瀏覽器之間又會掀起一場你死我活的爭斗,而這種爭斗可以使瀏覽器的解析標(biāo)準(zhǔn)會越來越規(guī)范,我們寫代碼也會越來越省事,這實在是一件值得高興的事。
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,一個是已經(jīng)選擇的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;
}
下面是在頁面中調(diào)用js代碼的方法:
<form action="a.cgi">
<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,在此謝過!
這段js代碼同時支持IE和Firefox,甚是精彩,可見此人js功力深厚啊,呵呵。
Btw:聽說Firefox現(xiàn)在的市場占有率已經(jīng)達(dá)到17%了,而IE8也快出來了,瀏覽器之間又會掀起一場你死我活的爭斗,而這種爭斗可以使瀏覽器的解析標(biāo)準(zhǔn)會越來越規(guī)范,我們寫代碼也會越來越省事,這實在是一件值得高興的事。
您可能感興趣的文章:
- js光標(biāo)定位文本框回車表單提交問題的解決方法
- js獲取光標(biāo)位置和設(shè)置文本框光標(biāo)位置示例代碼
- js實現(xiàn)在文本框光標(biāo)處添加字符的方法介紹
- javascript獲得光標(biāo)所在的文本框(text/textarea)中的位置
- JS在可編輯的div中的光標(biāo)位置插入內(nèi)容的方法
- js/html光標(biāo)定位的實現(xiàn)代碼
- 用javascript獲取當(dāng)頁面上鼠標(biāo)光標(biāo)位置和觸發(fā)事件的對象的代碼
- JavaScript 獲取/設(shè)置光標(biāo)位置,兼容Input&&TextArea
- javascript設(shè)置文本框光標(biāo)的方法實例小結(jié)
相關(guān)文章
封裝了一個支持匿名函數(shù)的Javascript事件監(jiān)聽器
這篇文章主要介紹了支持匿名函數(shù)的Javascript事件監(jiān)聽封裝,需要的朋友可以參考下2014-06-06
JavaScript 復(fù)制對象與Object.assign方法無法實現(xiàn)深復(fù)制
這篇文章主要介紹了JavaScript 復(fù)制對象與Object.assign方法無法實現(xiàn)深復(fù)制,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
js+css實現(xiàn)有立體感的按鈕式文字豎排菜單效果
這篇文章主要介紹了js+css實現(xiàn)有立體感的按鈕式文字豎排菜單效果,通過javascript動態(tài)調(diào)用頁面元素樣式實現(xiàn)豎排菜單的功能,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09
使用documentElement正確取得當(dāng)前可見區(qū)域的大小
如何取得當(dāng)前瀏覽器里面可見區(qū)域的大???其他方法都不適用,只有documentElement才可以,需要的朋友可以參考下2014-07-07

