用jquery写一个多引擎切换的代码 带logo
时间: 2023-04-07 15:04:49 浏览: 60
我可以回答这个问题。以下是一个使用jQuery编写的多引擎切换代码,带有logo:
HTML代码:
```
<div class="search-engines">
<a href="#" class="engine active" data-engine="google">
<img src="google-logo.png" alt="Google">
</a>
<a href="#" class="engine" data-engine="bing">
<img src="bing-logo.png" alt="Bing">
</a>
<a href="#" class="engine" data-engine="yahoo">
<img src="yahoo-logo.png" alt="Yahoo">
</a>
</div>
<form class="search-form">
<input type="text" name="q" placeholder="Search...">
<button type="submit">Search</button>
</form>
```
CSS代码:
```
.search-engines {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
.search-engines .engine {
margin-right: 10px;
}
.search-engines .engine:last-child {
margin-right: 0;
}
.search-engines .engine img {
width: 50px;
height: 50px;
}
.search-engines .engine.active img {
border: 2px solid #000;
}
.search-form {
display: flex;
justify-content: center;
}
.search-form input[type="text"] {
padding: 10px;
font-size: 16px;
border: 2px solid #000;
border-right: none;
border-radius: 5px 0 0 5px;
}
.search-form button {
padding: 10px;
font-size: 16px;
background-color: #000;
color: #fff;
border: 2px solid #000;
border-left: none;
border-radius: 0 5px 5px 0;
}
.search-form button:hover {
background-color: #fff;
color: #000;
}
```
jQuery代码:
```
$(document).ready(function() {
$('.engine').click(function(e) {
e.preventDefault();
$('.engine').removeClass('active');
$(this).addClass('active');
});
$('.search-form').submit(function(e) {
e.preventDefault();
var engine = $('.engine.active').data('engine');
var query = $('input[name="q"]').val();
var url;
switch (engine) {
case 'google':
url = 'https://www.google.com/search?q=' + encodeURIComponent(query);
break;
case 'bing':
url = 'https://www.bing.com/search?q=' + encodeURIComponent(query);
break;
case 'yahoo':
url = 'https://search.yahoo.com/search?p=' + encodeURIComponent(query);
break;
}
window.location.href = url;
});
});
```
这段代码创建了一个多引擎搜索框,用户可以选择不同的搜索引擎进行搜索。用户选择的搜索引擎会被标记为活动状态,并且在提交搜索表单时,会将查询字符串发送到所选的搜索引擎。