首页  >  文章  >  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删除