国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

javascript獲取以及設(shè)置光標(biāo)位置

2024-05-06 16:35:14
字體:
供稿:網(wǎng)友

一. javascript/28223.html">獲取光標(biāo)位置:

js;">// 獲取光標(biāo)位置function getCursortPosition (textDom) { var cursorPos = 0; if (document.selection) {  // IE Support  textDom.focus ();  var selectRange = document.selection.createRange();  selectRange.moveStart ('character', -textDom.value.length);  cursorPos = selectRange.text.length; }else if (textDom.selectionStart || textDom.selectionStart == '0') {  // Firefox support  cursorPos = textDom.selectionStart; } return cursorPos;}

二. 設(shè)置光標(biāo)位置:

// 設(shè)置光標(biāo)位置function setCaretPosition(textDom, pos){ if(textDom.setSelectionRange) {  // IE Support  textDom.focus();  textDom.setSelectionRange(pos, pos); }else if (textDom.createTextRange) {  // Firefox support  var range = textDom.createTextRange();  range.collapse(true);  range.moveEnd('character', pos);  range.moveStart('character', pos);  range.select(); }}

三. 獲取選中文字:

// 獲取選中文字function getSelectText() { var userSelection, text; if (window.getSelection) {  // Firefox support  userSelection = window.getSelection(); } else if (document.selection) {  // IE Support  userSelection = document.selection.createRange(); } if (!(text = userSelection.text)) {  text = userSelection; } return text;}

四. 選中特定范圍的文本:

/*** 選中特定范圍的文本* 參數(shù):*  textDom [JavaScript DOM String] 當(dāng)前對象*  startPos [Int] 起始位置*  endPos [Int] 終點(diǎn)位置*/function setSelectText(textDom, startPos, endPos) { var startPos = parseInt(startPos),  endPos = parseInt(endPos),  textLength = textDom.value.length; if(textLength){  if(!startPos){   startPos = 0;  }  if(!endPos){   endPos = textLength;  }  if(startPos > textLength){   startPos = textLength;  }  if(endPos > textLength){   endPos = textLength;  }  if(startPos < 0){   startPos = textLength + startPos;  }  if(endPos < 0){   endPos = textLength + endPos;  }  if(textDom.createTextRange){   // IE Support   var range = textDom.createTextRange();   range.moveStart("character",-textLength);   range.moveEnd("character",-textLength);   range.moveStart("character", startPos);   range.moveEnd("character",endPos);   range.select();  }else{   // Firefox support   textDom.setSelectionRange(startPos, endPos);   textDom.focus();  } }}

五. 在光標(biāo)后插入文本:

/*** 在光標(biāo)后插入文本* 參數(shù):*  textDom [JavaScript DOM String] 當(dāng)前對象*  value [String] 要插入的文本*/function insertAfterText(textDom, value) { var selectRange; if (document.selection) {  // IE Support  textDom.focus();  selectRange = document.selection.createRange();  selectRange.text = value;  textDom.focus(); }else if (textDom.selectionStart || textDom.selectionStart == '0') {  // Firefox support  var startPos = textDom.selectionStart;  var endPos = textDom.selectionEnd;  var scrollTop = textDom.scrollTop;  textDom.value = textDom.value.substring(0, startPos) + value + textDom.value.substring(endPos, textDom.value.length);  textDom.focus();  textDom.selectionStart = startPos + value.length;  textDom.selectionEnd = startPos + value.length;  textDom.scrollTop = scrollTop; } else {  textDom.value += value;  textDom.focus(); }}

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持VeVb武林網(wǎng)!


注:相關(guān)教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大方县| 视频| 宁武县| 武乡县| 日照市| 利津县| 二连浩特市| 屯门区| 西盟| 江达县| 普陀区| 屯门区| 天峻县| 霍邱县| 革吉县| 雷波县| 孝义市| 孟津县| 南部县| 平江县| 大姚县| 静宁县| 东乡| 莲花县| 静安区| 临城县| 新巴尔虎左旗| 鹤山市| 翼城县| 平利县| 霍邱县| 广元市| 苍南县| 北碚区| 精河县| 绍兴市| 体育| 祁阳县| 兴安盟| 奇台县| 五家渠市|