bin*_*nic 25 html forms webkit google-chrome
当用户点击字段时,我正在使用以下HTML代码在表单字段中自动选择一些文本:
<input onfocus="this.select()" type="text" value="Search">
Run Code Online (Sandbox Code Playgroud)
这在Firefox和Internet Explorer中工作正常(目的是使用默认文本向用户描述字段,但突出显示它以便在单击时他们可以开始键入),但是我无法让它在铬.当我单击Chrome中的表单字段时,文本会突然显示一瞬间,然后光标跳转到默认文本的末尾,突出显示消失.
有关如何在Chrome中使用此功能的任何想法?
Bak*_*yor 22
您必须将此操作绑定到onclick事件,而不是绑定到onfocus事件,它将按您的意愿工作.
<input onclick="this.select()" id="txt1" name="txt1" type="text" value="Search">
Run Code Online (Sandbox Code Playgroud)
小智 5
这对我来说效果最好......
<input type="text" onfocus="this.searchfocus = true;" onmouseup="if(this.searchfocus) {this.select(); this.searchfocus = false;}" />
Run Code Online (Sandbox Code Playgroud)
onfocus后,mouseup事件将触发.