js獲取光標位置和設置文本框光標位置示例代碼
<script type="text/javascript">
function getTxt1CursorPosition(){
var oTxt1 = document.getElementById("txt1");
var cursurPosition=-1;
if(oTxt1.selectionStart){//非IE瀏覽器
cursurPosition= oTxt1.selectionStart;
}else{//IE
var range = document.selection.createRange();
range.moveStart("character",-oTxt1.value.length);
cursurPosition=range.text.length;
}
alert(cursurPosition);
}
function setTxt1CursorPosition(i){
var oTxt1 = document.getElementById("txt2");
var cursurPosition=-1;
if(oTxt1.selectionStart){//非IE瀏覽器
oTxt1.selectionStart=i;
}else{//IE
var range = oTxt1.createTextRange();
range.move("character",i);
range.select();
}
}
function getTa1CursorPosition(){
var evt =window.event?window.event:getTa1CursorPosition.caller.arguments[0];
var oTa1 = document.getElementById("ta1");
var cursurPosition=-1;
if(oTa1.selectionStart){//非IE瀏覽器
cursurPosition= oTa1.selectionStart;
}else{//IE
var range = oTa1.createTextRange();
range.moveToPoint(evt.x,evt.y);
range.moveStart("character",-oTa1.value.length);
cursurPosition=range.text.length;
}
alert(cursurPosition);
}
function setTa1CursorPosition(i){
var oTa2 = document.getElementById("ta2");
if(oTa2.selectionStart){//非IE瀏覽器
oTa2.selectionStart=i;
oTa2.selectionEnd=i;
}else{//IE
var range = oTa2.createTextRange();
range.move("character",i);
range.select();
}
}
</script>
相關文章
input+select(multiple) 實現(xiàn)下拉框輸入值
昨天做一個網(wǎng)站時,需要實現(xiàn)下拉框能夠輸入,從功能上講是要實現(xiàn)用戶在文本框輸入值時,能夠從后讀出數(shù)據(jù)彈出下拉選項2009-05-05
多種方式實現(xiàn)JS調(diào)用后臺方法進行數(shù)據(jù)交互
幾種典型常用的方法如利用控件的AutopostBack屬性、Button提交表單等等,下面為大家分享下JS調(diào)用后臺方法進行數(shù)據(jù)交互示例2013-08-08
JS組件Bootstrap Table表格多行拖拽效果實現(xiàn)代碼
這篇文章主要介紹了JS組件Bootstrap Table表格多行拖拽效果實現(xiàn)代碼,需要的朋友可以參考下2015-12-12
滾動條的監(jiān)聽與內(nèi)容隨著滾動條動態(tài)加載的實現(xiàn)
下面小編就為大家?guī)硪黄獫L動條的監(jiān)聽與內(nèi)容隨著滾動條動態(tài)加載的實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

