js设置光标位置
2016-06-29来源:

function getcaret(textbox)

{

var control = document.activeelement;

textbox.focus();

var rang = document.selection.createrange();

rang.setendpoint(starttostart,textbox.createtextrange())

control.focus();

return rang.text.length;

}

function setcaret(textbox,pos)

{

try

{

var r =textbox.createtextrange();

r.movestart('character',pos);

r.collapse(true);

r.select();

}

catch(e)

{}

}

function selectlength(textbox,start,len)

{

try

{

var r =textbox.createtextrange();

r.moveend('character',len-(textbox.value.length-start));

r.movestart('character',start);

r.select();

}

catch(e)

{//alert(e.description)}

}

function insertatcaret(textbox,text)

{

textbox.focus();

document.selection.createrange().text = text;

}

推荐信息
Baidu
map