开发者

Setting marker position inside textarea

I'm looking for a way to set the tex开发者_JAVA技巧t marker to the beginning of a textarea when there's a value set or text between the textarea tags. I couldn't find anything on it when searching. So, does anyone know how to go about doing this?


var el = document.getElementById("myTextArea"); 

if (typeof el.setSelectionRange != "undefined") {
    el.setSelectionRange(0, 0);
} else if (typeof el.createTextRange != "undefined") {
    var range = el.createTextRange();
    range.collapse(true);
    range.select();
}


The following should be something like what you're looking for, although I haven't tested it.

var el = document.getElementById("myTextArea"); 

// IE
if (document.selection) {
    var sel = el.createTextRange();
    sel.moveStart("character", 0);
} 
// Others
else if ("setSelectionRange" in el) {
    el.setSelectionRange(0, 0);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜