使用者在 HTML 中的搜尋欄位中書寫內容時執行指令碼?
在 HTML 中使用 onsearch 屬性,以便當使用者在搜尋欄位中書寫內容並按 ENTER 或 x 時執行指令碼。
示例
你可以嘗試執行以下程式碼來實現 onsearch 屬性 −
<!DOCTYPE html> <html> <body> <p>Search something and press ENTER.</p> <input type = "search" id="inputID" onsearch = "display()"> <p><strong>Note:</strong> It won' work in Internet Explorer and Firefox.</p> <p id="test"></p> <script> function display() { var a = document.getElementById("inputID"); document.getElementById("test").innerHTML = "Searched for - " + a.value; } </script> </body> </html>
廣告