使用js原生实现一个搜索框
时间: 2024-06-06 14:07:26 浏览: 23
<!DOCTYPE html>
<html>
<head>
<title>搜索框</title>
<meta charset="utf-8">
<style type="text/css">
#search-box {
width: 300px;
height: 30px;
padding: 5px;
border: 1px solid #ccc;
border-radius: 5px;
outline: none;
}
#search-btn {
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 5px;
padding: 5px 10px;
cursor: pointer;
}
#search-btn:hover {
background-color: #3e8e41;
}
</style>
</head>
<body>
<input type="text" id="search-box" placeholder="请输入关键字">
<button id="search-btn">搜索</button>
<script type="text/javascript">
var searchBox = document.getElementById('search-box');
var searchBtn = document.getElementById('search-btn');
searchBtn.onclick = function() {
var keyword = searchBox.value;
alert('搜索关键字是:' + keyword);
}
</script>
</body>
</html>
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)