可以使用 JavaScript 選擇文字框嗎?
是的,可以使用 JavaScript 中的 select() 方法選擇文字框。首先,讓我們建立一個文字輸入 -
Enter your Name:<input type="text" id="txtName" value="John"> <br> <button type="button" onclick="check()">Select Text Box</button>
現在,讓我們單擊按鈕選擇文字框 -
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initialscale=1.0"> <title>Document</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> </head> <body> Enter your Name:<input type="text" id="txtName" value="John"> <br> <button type="button" onclick="check()">Select Text Box</button> <script> function check(){ document.getElementById("txtName").select(); } </script> </body> </html>
要執行上述程式,請儲存檔名為 “anyName.html(index.html)” 並右鍵單擊該檔案。在 VS Code 編輯器中選擇“使用 Live Server 開啟”。
輸出
這將生成以下輸出 -
單擊按鈕“選擇文字框”後,它將選擇文字框。這將生成以下輸出 -
廣告