简体   繁体   English

函数setSelectionRange在Android浏览器中不起作用

[英]Function setSelectionRange is not working in Android browser

I am trying to do something like this in a directive in Angularjs. 我试图在Angularjs的指令中做这样的事情。 The problem is this works fine in system but when I check the same in Android phone it is failing to set the caret position. 问题是这在系统中工作正常,但是当我在Android手机中检查相同时,它无法设置插入位置。 I checked on several blogs and website also, is there a solution for this in android device? 我在几个博客和网站上查了一下,Android设备中有这个解决方案吗?

elem.bind("input", function(event) {
       var start = elem[0].selectionStart;
       if(start == 1 && event.currentTarget.value.charAt(start-1)==" "){
       var value = event.currentTarget.value.trim();
       event.currentTarget.value = value;
       elem[0].setSelectionRange(0, 0);
    }
}

And yes if I don't use setSelectionRange it moves the caret to the end. 是的,如果我不使用setSelectionRange,它会将插入符号移到最后。

i had the same problem, i used this advice and it worked for me Why the setSelectionRange can't work on android 2.3 platform browser 我有同样的问题,我使用这个建议,它对我有用为什么setSelectionRange无法在Android 2.3平台浏览器上工作

try wrapping your "setSelectionRange" with "setTimeout with an interval of 0" 尝试用“setTimeout,间隔为0”包装“setSelectionRange”

tha't should fix your problem. 不应该解决你的问题。

for example: 例如:

   var start = elem[0].selectionStart;
   if(start == 1 && event.currentTarget.value.charAt(start-1)==" "){
   var value = event.currentTarget.value.trim();
   event.currentTarget.value = value;
   setTimeout(() => elem[0].setSelectionRange(0, 0),0);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM