什么是香草JS或jQuery解决方案,将选择一个文本框的所有内容时,文本框接收焦点?


当前回答

onclick="this.focus();this.select()"

其他回答

这对我来说很管用(因为它不是在回答中,而是在评论中)

 $("#textBox").focus().select();

注意:如果你在ASP中编程。NET中,您可以使用ScriptManager运行脚本。c#中的RegisterStartupScript:

ScriptManager.RegisterStartupScript(txtField, txtField.GetType(), txtField.AccessKey, "$('#MainContent_txtField').focus(function() { $(this).select(); });", true );

或者在其他答案中建议的HTML页面中输入脚本。

如果你把这些事件链接在一起,我相信就不需要使用.one了,就像本文其他地方建议的那样。

例子:

$('input.your_element').focus( function () {
    $(this).select().mouseup( function (e) {
        e.preventDefault();
    });
});

这个会有用的,试试这个

<input id="textField1" onfocus="this.select()" onmouseup="return false" /> 

可以在Safari/IE 9和Chrome上运行,但我没有机会在Firefox上测试。

这也适用于iOS:

<input type="text" onclick="this.focus(); this.setSelectionRange(0, 9999);" />

https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/select