首頁  >  文章  >  web前端  >  當使用者在 HTML 搜尋欄位中寫入內容時執行腳本?

當使用者在 HTML 搜尋欄位中寫入內容時執行腳本?

王林
王林轉載
2023-08-28 13:21:071000瀏覽

当用户在 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&#39; 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>

以上是當使用者在 HTML 搜尋欄位中寫入內容時執行腳本?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除