在某些浏览器上,文本会在元素焦点上自动选择
我有这个自动搜索文本输入,当用户键入字母时,它会进行新的搜索。现在,每次搜索后我都必须关注此元素,但我在除 Firefox 之外的所有浏览器中,都会选择输入中的文本。如果用户键入另一个字母,则所选文本将被删除,这意味着用户必须先取消选择文本,然后才能键入另一个字母。当然,用户会输入新字母并认为该程序有问题。
是否可以取消选择焦点上的文本或从一开始就阻止这种选择发生?
I have this automatically searching text input, which makes a new search when user types a letter. Now I have to focus to this element after every search, but I in all browsers except Firefox, text in input is selected. If user types another letter, then selected text is erased and this will mean that user must unselect text before user can type in another letter. Of course user will type new letter and think that there is something wrong with this program.
Is it possible to unselect text on focus or prevent this selection happening at the first place?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在焦点上将文本框的值分配给自身将取消选择文本。
工作演示
Assigning the value of the textbox to itself on focus will unselect the text.
Working Demo
我不太确定,但我认为这可能是一个解决方案,或者只需使用属性
onselect="return;"
I am not too sure but I think this could be a solution or simply use the attribute
onselect="return;"
我用下面的代码让它工作:
I got it working with following code: