用html和js写一个搜索框可以自定义搜索引擎简约风扁平化
时间: 2023-05-27 12:02:11 浏览: 129
html+jquery简洁美观的搜索框
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>自定义搜索引擎</title>
<style>
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
.container {
margin: 50px auto;
width: 500px;
}
.search-form {
background-color: #fff;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
padding: 10px;
position: relative;
}
.search-form input[type="text"] {
border: none;
border-radius: 5px;
font-size: 16px;
padding: 5px 10px;
width: 80%;
}
.search-form button {
background-color: #1abc9c;
border: none;
border-radius: 5px;
color: #fff;
cursor: pointer;
font-size: 16px;
padding: 5px 10px;
position: absolute;
right: 0;
top: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="search-form">
<input type="text" id="searchBox" placeholder="在这里输入要搜索的内容">
<button onclick="search()">搜索</button>
</div>
</div>
<script>
function search() {
var searchBox = document.getElementById("searchBox");
var searchEngine = document.querySelector('input[name="searchEngine"]:checked').value;
var query = encodeURIComponent(searchBox.value);
var url;
switch(searchEngine) {
case "google":
url = "https://www.google.com/search?q=" + query;
break;
case "baidu":
url = "https://www.baidu.com/s?wd=" + query;
break;
case "bing":
url = "https://www.bing.com/search?q=" + query;
break;
default:
url = "https://www.google.com/search?q=" + query;
}
window.location.href = url;
}
</script>
<div class="container">
<form>
<input type="radio" id="google" name="searchEngine" value="google" checked>
<label for="google">Google</label>
<input type="radio" id="baidu" name="searchEngine" value="baidu">
<label for="baidu">Baidu</label>
<input type="radio" id="bing" name="searchEngine" value="bing">
<label for="bing">Bing</label>
</form>
</div>
</body>
</html>
阅读全文