加載頁面時我想將光標移至特定字段。沒問題。但我還需要選擇並突出顯示放置在該文本字段中的默認值。如何在頁面加載時自動選擇輸入字段及其中的文本
50
A
回答
81
從http://www.codeave.com/javascript/code.asp?u_log=7004:
var input = document.getElementById('myTextInput');
input.focus();
input.select();
<input id="myTextInput" value="Hello world!" />
14
要做到這一點在頁面加載:
27
在你輸入標籤,放置以下:
onFocus="this.select()"
+0
這隻適用於FireFox。在Chrome中,您可以通過添加「onmouseup ='return false'」來解決此問題,但這不會解決Edge中的問題,如果您使用鍵盤導航到元素,也不能解決問題。 – Vincent 2015-09-09 23:17:48
5
我發現效果很好一個非常簡單的方法:在這裏
-1
<input type="text" value="test" onclick="this.select()">
測試試試這個。這將適用於Firefox和Chrome。
<input type="text" value="test" autofocus="autofocus" onfocus="this.select()">
14
<input type="text" onclick="this.focus();this.select()">
0
使用jQuery的時候...
HTML:
<input type='text' value='hello world' id='hello-world-input'>
的jQuery:
$(function() {
$('#hello-world-input').focus().select();
});
相關問題
- 1. 如何在頁面加載時清空輸入字段WITH JAVASCRIPT
- 2. 如何自動選擇文本輸入?
- 3. 如何在自舉彈出框內自動選擇輸入字段的文本
- 4. 使用jQuery自動選擇頁面加載的文本框
- 5. 在其他文本輸入字段中輸入文本時啓用複選框
- 6. SelectedIndex自動選擇頁面加載
- 7. Android - 選擇/輸入字段時自動填充字段
- 8. jQuery的日期選擇器 - 填補在頁面加載輸入字段
- 9. 自動選擇輸入字段
- 10. 處理「其他」選項在下拉選擇與自由文本輸入字段
- 11. 在頁面加載時檢查輸入字段?
- 12. 當下拉打開時,自動選擇輸入字段的文本
- 13. 如何在頁面加載時從下拉列表中自動選擇項目
- 14. 如何在javascript測驗頁面中添加其他(輸入文本選項)
- 15. 如何在選擇輸入文本字段時刪除android中的邊框?
- 16. 在頁面加載時選擇自動完成值
- 17. 如果在多個選擇輸入中選擇了「其他」,Javascript顯示/隱藏文本字段輸入
- 18. 重力形式 - 在選擇時自動清除輸入字段?
- 19. 如何在輸入字段上重新啓用文本選擇
- 20. 在頁面加載時在文本字段中插入當前網址
- 21. 使用Ajax和PHP選擇輸入,如何在頁面加載時調用ajax?
- 22. 如何選擇tabindex =「0」時啓動頁面加載時
- 23. 在頁面加載時動態加載選擇框
- 24. Formtastic:在輸入字段中插入預加載的文本
- 25. 如何在文本字段中添加自動刪除選項
- 26. 如何在電話頁面加載時專注於輸入字段?
- 27. 如何選擇頁面加載選項
- 28. 如何獲取輸入文本字段在選擇框中的值選項值
- 29. 如何在選擇值時將值加載到文本字段中
- 30. 如何檢測時,文本在輸入字段動態插入
這適用於Firefox,但不會選擇Chrome或Edge中的文本。 – Vincent 2015-09-09 23:19:52
對焦法似乎工作正常 – edward 2016-04-22 07:42:59