admin管理员组文章数量:1419235
I have a function that selects the first 5 characters of a input onclick. This works for the very first click. When the input box still has focus, in chrome, a second click within the first 5 characters removes the selection and the caret is blinking at the position of the click.
I expect the first 5 characters to still-be/remain selected.
This appears to be a chrome issue as it works fine in Firefox.
Here is my fiddle: /
How can I get this working with chrome?
I have tried resetting the selection using
sender.setSelectionRange(0, 1);
sender.setSelectionRange(0, 5);
I have a function that selects the first 5 characters of a input onclick. This works for the very first click. When the input box still has focus, in chrome, a second click within the first 5 characters removes the selection and the caret is blinking at the position of the click.
I expect the first 5 characters to still-be/remain selected.
This appears to be a chrome issue as it works fine in Firefox.
Here is my fiddle: http://jsfiddle/valamas/3eaYq/2/
How can I get this working with chrome?
I have tried resetting the selection using
sender.setSelectionRange(0, 1);
sender.setSelectionRange(0, 5);
Share
Improve this question
asked Aug 21, 2012 at 2:19
ValamasValamas
24.7k25 gold badges110 silver badges181 bronze badges
1 Answer
Reset to default 7This is indeed an issue with Chromium (and a two year old issue at that). To make Chrome behave the same as Firefox, simply wrap the contents of doSelect
in a setTimeout
call:
function doSelect(sender, e) {
setTimeout(function(){
sender.setSelectionRange(0, 5);
}, 0);
}
See also: the updated JS Fiddle
Update: The bug is now marked as FIXED
and will be delivered in Chrome 39.
本文标签: javascriptsetSelectionRange with on click in chrome does not select on second clickStack Overflow
版权声明:本文标题:javascript - setSelectionRange with on click in chrome does not select on second click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745300532a2652346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论