javascript获取以及设置光标位置
程序员文章站
2023-11-18 22:56:04
一. 获取光标位置:
// 获取光标位置
function getcursortposition (textdom) {
var cursorpos = 0...
一. 获取光标位置:
// 获取光标位置 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; }
二. 设置光标位置:
// 设置光标位置 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; }
四. 选中特定范围的文本:
/** * 选中特定范围的文本 * 参数: * textdom [javascript dom string] 当前对象 * startpos [int] 起始位置 * endpos [int] 终点位置 */ 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(); } } }
五. 在光标后插入文本:
/** * 在光标后插入文本 * 参数: * textdom [javascript dom string] 当前对象 * 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(); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!