`
cuijiemin
  • 浏览: 265395 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

FF IE 得到input.text textarea 光标,更改光标位置 备忘

IE 
阅读更多

function inputkeyup(obj,event,value){
if(event.keyCode!=37&&event.keyCode!=39&&event.keyCode!=46&&event.keyCode!=8){
newvalue=value.replace(/[<>]/g,'');
//如果输入错误,退格并且定位光标位置
if(value!=newvalue){
if(document.selection){
if(obj.type == 'textarea'){
var slen = 0;
var nlen = value.length-newvalue.length;
var sTextRange=document.selection.createRange();
var oTextRange = document.body.createTextRange();
oTextRange.moveToElementText(obj);
for (start=0; oTextRange.compareEndPoints("StartToStart", sTextRange) < 0; slen++){
oTextRange.moveStart('character', 1);
}
obj.value=newvalue;
var a =obj.createTextRange();
a.moveStart('character',slen-nlen);
a.collapse(true);
a.select();
}else{
var s=document.selection.createRange();
s.setEndPoint("StartToStart",obj.createTextRange());
var slen = s.text.length;
var nlen = value.length-newvalue.length;
obj.value=newvalue;
var a =obj.createTextRange();
a.moveStart('character',slen-nlen);
a.collapse(true);
a.select();
}
}else{
var slen = obj.selectionStart;
var nlen = value.length-newvalue.length;
obj.value=newvalue;
obj.selectionStart=slen-nlen;
obj.selectionEnd=slen-nlen;
}
}
}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics