HTML onsearch 事件屬性
當用戶在 HTML 文件中使用 type=”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 事件屬性如何工作。

廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP