HTML onsearch 事件属性
htmlweb developmentfront end technology更新于 2025/12/21 0:12:17
当用户在 HTML 文档中,类型为"text"的 HTML 输入元素中按下"Enter"键时,会触发 HTML onsearch 事件属性。
语法
以下是语法 −
<input type=”search” onsearch=”script”>
让我们看一个 HTML onsearch 事件属性的示例 −
示例
<!DOCTYPE html>
<html>
<head>
<style>
body {
color: #000;
height: 100vh;
background: linear-gradient(62deg, #FBAB7E 0%, #F7CE68 100%) no-repeat;
text-align: center;
padding: 20px;
}
input {
border: 2px solid #fff;
background: transparent;
height: 2rem;
padding: 10px;
width: 200px;
}
::placeholder {
color: #000;
}
.show {
font-size: 1.5rem;
font-weight: bold;
}
</style>
</head>
<body>
<h1>HTML onsearch Event Attribute Demo</h1>
<input type="search" onsearch="searchFn()" placeholder="Search here">
<p>Enter what you want to search and then hit "Enter" key</p>
<div class="show"></div>
<script>
function searchFn() {
document.body.style.background = "linear-gradient(45deg, #8BC6EC 0%, #9599E2 100%) no-repeat";
document.querySelector(".show").innerHTML = "You search: " + document.querySelector("input").value;
}
</script>
</body>
</html>
输出

在搜索字段中输入您的搜索文本,然后按"Enter"键以观察 onsearch 事件属性如何工作。

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

