1:实现火狐切换搜索的功能,当选择哪个搜索,切换对应公司的logo图片,并且更改表单的action属性,当输入值并点击搜索的时候
时间: 2024-06-08 17:10:50 浏览: 25
,会跳转到对应的搜索引擎页面。
实现这个功能需要以下步骤:
1. 在页面上创建搜索框和搜索按钮,并为它们设置相应的 id 和 name 属性。
2. 为每个搜索引擎创建一个 logo 图片,并在页面上显示出来。
3. 为每个搜索引擎创建一个 form 表单,并设置不同的 action 属性,以便在提交时跳转到对应的搜索引擎页面。
4. 使用 JavaScript 代码监听搜索框和搜索按钮的事件,并根据用户选择切换 logo 图片和表单的 action 属性。
5. 当用户输入关键字并点击搜索按钮时,将表单提交到对应的搜索引擎页面。
以下是代码实现:
HTML 代码:
```html
<div>
<form id="searchForm" action="" method="get">
<input type="text" id="searchInput" name="q" placeholder="Search...">
<button type="submit" id="searchBtn">Search</button>
</form>
</div>
<div>
<img id="googleLogo" src="google_logo.png" alt="Google" style="display: none;">
<img id="bingLogo" src="bing_logo.png" alt="Bing" style="display: none;">
</div>
```
JavaScript 代码:
```javascript
const searchForm = document.getElementById('searchForm');
const searchInput = document.getElementById('searchInput');
const searchBtn = document.getElementById('searchBtn');
const googleLogo = document.getElementById('googleLogo');
const bingLogo = document.getElementById('bingLogo');
// 监听搜索按钮点击事件
searchBtn.addEventListener('click', function(event) {
event.preventDefault(); // 阻止表单默认提交行为
const selectedEngine = document.querySelector('input[name="engine"]:checked').value;
const query = searchInput.value;
searchForm.action = getEngineUrl(selectedEngine, query);
searchForm.submit();
});
// 监听搜索引擎选择事件
document.querySelectorAll('input[name="engine"]').forEach(function(engine) {
engine.addEventListener('change', function() {
const selectedEngine = this.value;
googleLogo.style.display = selectedEngine === 'google' ? 'block' : 'none';
bingLogo.style.display = selectedEngine === 'bing' ? 'block' : 'none';
searchForm.action = getEngineUrl(selectedEngine, searchInput.value);
});
});
// 根据搜索引擎和关键字获取搜索 URL
function getEngineUrl(engine, query) {
switch (engine) {
case 'google':
return `https://www.google.com/search?q=${encodeURIComponent(query)}`;
case 'bing':
return `https://www.bing.com/search?q=${encodeURIComponent(query)}`;
default:
return '';
}
}
```
在这个代码中,我们首先从 HTML 页面中获取了搜索框、搜索按钮和 logo 图片的 DOM 元素,并为它们添加了相应的 id 和 name 属性。然后我们监听了搜索按钮的点击事件以及搜索引擎选择事件,并编写了对应的事件处理函数。在搜索按钮点击事件中,我们首先阻止了表单的默认提交行为,然后获取用户选择的搜索引擎和搜索关键字,并将表单的 action 属性设置为对应的搜索引擎 URL,最后提交表单。在搜索引擎选择事件中,我们根据用户选择切换 logo 图片的显示和隐藏,并重新设置表单的 action 属性。
这样,当用户选择不同的搜索引擎并输入关键字后,点击搜索按钮就会跳转到对应的搜索引擎页面了。
相关推荐
![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)