单击时选择HTML文本输入中的所有文本 [英] Selecting all text in HTML text input when clicked
问题描述
我有以下代码在HTML网页中显示文本框。
I have the following code to display a textbox in a HTML webpage.
<input type="text" id="userid" name="userid" value="Please enter the user ID" />
当页面显示时,文本包含请输入用户ID 信息。但是,我发现用户需要点击3次以选择所有文本(在这种情况下,它是请输入用户ID )。
When the page displays, the text contains the Please enter the user ID message. However, I found that the user needs to click 3 times in order to select all the text (in this case it is Please enter the user ID).
只需点击一下就可以选择整个文本吗?
Is it possible to select the entire text with only one click?
编辑:
对不起,我忘了说:我必须使用输入 type =text
Sorry, I forgot to say: I must use the input type="text"
推荐答案
您可以使用以下javascript代码片段:
You can use this javascript snippet:
<input onClick="this.select();" value="Sample Text" />
但显然它不适用于移动版Safari。在这些情况下,您可以使用:
But apparently it doesn't work on mobile Safari. In those cases you can use:
<input onClick="this.setSelectionRange(0, this.value.length)" value="Sample Text" />
这篇关于单击时选择HTML文本输入中的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!