溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Javascript中如何利用textarea獲取光標(biāo)位置

發(fā)布時(shí)間:2022-05-06 15:44:02 來(lái)源:億速云 閱讀:173 作者:iii 欄目:大數(shù)據(jù)

這篇“Javascript中如何利用textarea獲取光標(biāo)位置”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“Javascript中如何利用textarea獲取光標(biāo)位置”文章吧。

用Javascript獲取textarea中的光標(biāo)位置

Javascript一向以他的靈活隨意而著稱(chēng),這也使得它的功能可以非常的強(qiáng)大,而由于沒(méi)有比較好的調(diào)試工具,又使得它使用起來(lái)困難重重,尤其使對(duì)于一些初學(xué)者,更是感覺(jué)到無(wú)從下手。今天探討的問(wèn)題是用javascript獲取textarea中光標(biāo)的位置。對(duì)于寫(xiě)javascript寫(xiě)網(wǎng)頁(yè)編輯器的人來(lái)說(shuō),獲取textarea中的光標(biāo)位置是一個(gè)非常重要的問(wèn)題,而往往很多人在這個(gè)地方不知所措,找不到好的辦法。昨天我在網(wǎng)上找到了一段javascript代碼,本來(lái)不想把原版放在這里的,就是因?yàn)樘柿?,怕我給改壞了,所以還是原版放在這里吧。

varstart=0;  varend=0;  functionadd(){  vartextBox=document.getElementById("ta");  varpre=textBox.value.substr(0,start);  varpost=textBox.value.substr(end);  textBox.value=pre+document.getElementById("inputtext").value+post;  }  functionsavePos(textBox){  //如果是Firefox(1.5)的話,方法很簡(jiǎn)單  if(typeof(textBox.selectionStart)=="number"){  start=textBox.selectionStart;  end=textBox.selectionEnd;  }  //下面是IE(6.0)的方法,麻煩得很,還要計(jì)算上'\n'  elseif(document.selection){  varrange=document.selection.createRange();  if(range.parentElement().id==textBox.id){  //createaselectionofthewholetextarea  varrange_all=document.body.createTextRange();  range_all.moveToElementText(textBox);  //兩個(gè)range,一個(gè)是已經(jīng)選擇的text(range),  一個(gè)是整個(gè)textarea(range_all)  //range_all.compareEndPoints()比較兩個(gè)端點(diǎn),  如果range_all比range更往左(furthertotheleft),  則//返回小于0的值,則range_all往右移一點(diǎn),直到兩個(gè)range的start相同。  //calculateselectionstartpointbymovingbeginningofrange_alltobeginningofrange  for(start=0;range_all.compareEndPoints("StartToStart",range)<0;start++)range_all.moveStart('character',1);  //getnumberoflinebreaksfromtextareastarttoselectionstartandaddthemtostart  //計(jì)算一下\n  for(vari=0;i<=start;i++){  if(textBox.value.charAt(i)=='\n')  start++;  }  //createaselectionofthewholetextarea  varrange_all=document.body.createTextRange();  range_all.moveToElementText(textBox);  //calculateselectionendpointbymovingbeginningofrange_alltoendofrange  for(end=0;range_all.compareEndPoints('StartToEnd',range)<0;end++)  range_all.moveStart('character',1);  //getnumberoflinebreaksfromtextareastarttoselectionendandaddthemtoend  for(vari=0;i<=end;i++){  if(textBox.value.charAt(i)=='\n')  end++;  }  }  }  document.getElementById("start").value=start;  document.getElementById("end").value=end;  }

下面是在頁(yè)面中調(diào)用js代碼的方法:

<formactionformaction="a.cgi"> <tablebordertableborder="1"cellspacing="0"cellpadding="0"> <tr> <td>start:<inputtypeinputtype="text"id="start"size="3"/></td> <td>end:<inputtypeinputtype="text"id="end"size="3"/></td> </tr> <tr> <tdcolspantdcolspan="2"> <textareaidtextareaid="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><inputtypeinputtype="text"id="inputtext"/></td> <td><inputtypeinputtype="button"onClick="add()"value="AddText"/></td> </tr> </table> </form>

以上就是關(guān)于“Javascript中如何利用textarea獲取光標(biāo)位置”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI