javascript textarea 光标定位

来源:互联网 发布:sql视频 编辑:程序博客网 时间:2024/06/07 06:02

 javascript textarea 光标定位到具体的位置 如果加载的内容是ajax 最好用setTimeout函数包装一下:通用各个浏览器

setTimeout(function() {
       var tea = document.getElementById(id);
   if (tea.setSelectionRange !==undefined && tea.setSelectionRange) {
  tea.setSelectionRange(0, 0); 
  tea.focus();
  
  }else if (tea.createTextRange) {
  var txt=tea.createTextRange();
  txt.moveStart("character",0);
  txt.collapse(true);
  txt.select();
  }
 }, 1000);

0 0