我有一个函数可以选择输入 onclick 的前 5 个字符。这适用于第一次点击。当输入框仍然有焦点时,在 chrome 中,前 5 个字符内的第二次单击会删除选择,插入符号会在单击的位置闪烁。
我希望前 5 个字符仍处于选中状态。
这似乎是一个 chrome 问题,因为它在 Firefox 中运行良好。
这是我的 fiddle : http://jsfiddle.net/valamas/3eaYq/2/
我怎样才能让它与 chrome 一起工作?
我尝试使用
重置选择sender.setSelectionRange(0, 1);
sender.setSelectionRange(0, 5);
最佳答案
这确实是 an issue with Chromium (那是一个两年前的问题)。要使 Chrome 的行为与 Firefox 相同,只需将 doSelect
的内容包装在 setTimeout
调用中:
function doSelect(sender, e) {
setTimeout(function(){
sender.setSelectionRange(0, 5);
}, 0);
}
更新:该错误现已标记为已修复
,并将在 Chrome 39 中提供。
关于javascript - 在 chrome 中点击 setSelectionRange 不会在第二次点击时选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12047648/