有一些文本输入。输入聚焦时(第一次单击输入),必须选择此输入中的所有文本;第二次单击输入时,必须选择特定的单词。
我尝试实现与Chrome(版本74.0.3729.131(正式版本)(64位))中的URL栏相同的功能。
您可以在此处查看输入的当前行为:https : //jsfiddle.net/deaamod1s/rh5mw0e4/23/
我看到的唯一解决方案是检查是否双击输入,然后如果未双击输入,则执行此操作 input.select()
input.onfocus = function(e) {
let hasValueWhenGotFocused = false;
if (input.value) {
hasValueWhenGotFocused = true;
}
setTimeout(() => {
if (hasValueWhenGotFocused && this.className.indexOf("doubleclicked") == -1) {
this.select();
} else {
this.classList.remove("doubleclicked");
}
},400);
}
input.ondblclick = function(e){
this.classList.add('doubleclicked');
}
相关分类