javascript: focus()文本框文字最后获得焦点光标,兼容多浏览器

时间: 2015-03-18  分类: Javascript  收藏

可兼容火狐FF, IE浏览器,谷歌浏览器,其它浏览器基于IE内核应该也支持

cobj = document.getElementById("comment"); //
cobj.value='内容 '+cont+':'; //加上内容
//cobj.focus();
cobj.focus(); //得到焦点
var len = cobj.value.length;
if(document.selection) {
    var sel = cobj.createTextRange();
    sel.moveStart('character',len);
    sel.collapse();
    sel.select();
} else if (typeof cobj.selectionStart == 'number' && typeof cobj.selectionEnd == 'number') {
    cobj.selectionStart = cobj.selectionEnd = len;
}
分享到:

评论

昵 称: