当用户在 HTML 的搜索字段中输入内容时执行脚本?
htmlweb developmentfront end technology更新于 2025/8/16 2:20:17
使用 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>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

