原生js写出搜索框搜索
时间: 2023-09-22 22:10:08 浏览: 55
好的,以下是一个基本的搜索框搜索的实现:
HTML部分:
```html
<div>
<input type="text" id="search-input" placeholder="请输入关键词">
<button id="search-btn">搜索</button>
</div>
```
JS部分:
```js
var searchBtn = document.getElementById('search-btn');
var searchInput = document.getElementById('search-input');
searchBtn.addEventListener('click', function() {
var keyword = searchInput.value;
if (keyword) {
window.location.href = 'https://www.baidu.com/s?wd=' + keyword;
}
});
```
这段代码会监听搜索按钮的点击事件,当点击时获取搜索框中输入的关键词,然后将其作为参数拼接到百度搜索的URL后面,最后跳转到搜索结果页。当然,这只是一个简单的实现,实际情况需要考虑更多的因素,比如防止XSS攻击,处理用户输入的特殊字符等。
相关问题
使用安卓原生写一个搜索框
好的,以下是一个使用安卓原生写的搜索框的示例代码:
```
<EditText
android:id="@+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"
android:inputType="text"
android:maxLines="1"
android:singleLine="true" />
```
这是一个简单的 EditText 组件,用户可以在其中输入搜索关键字。你可以将其放置在你的布局文件中,并在你的代码中使用 findViewById() 方法来获取对它的引用。然后,你可以在你的代码中监听用户的输入,并使用它来执行搜索操作。
使用原生js写一个下拉框且可以搜索
下面是一个使用原生JS编写的带搜索功能的下拉框代码示例:
HTML:
```html
<div class="dropdown">
<input type="text" id="searchInput" placeholder="Search...">
<button class="dropbtn">Select item</button>
<div class="dropdown-content">
<a href="#">Item 1</a>
<a href="#">Item 2</a>
<a href="#">Item 3</a>
<a href="#">Item 4</a>
<a href="#">Item 5</a>
<a href="#">Item 6</a>
<a href="#">Item 7</a>
<a href="#">Item 8</a>
<a href="#">Item 9</a>
<a href="#">Item 10</a>
</div>
</div>
```
CSS:
```css
.dropdown {
position: relative;
display: inline-block;
}
#searchInput {
padding: 5px;
font-size: 16px;
border: none;
border-bottom: 1px solid #ccc;
}
.dropdown-content {
display: none;
position: absolute;
z-index: 1;
background-color: #f9f9f9;
min-width: 160px;
max-height: 200px;
overflow-y: auto;
border: 1px solid #ddd;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.show {
display: block;
}
```
JS:
```javascript
// Get the button and dropdown div
const btn = document.querySelector(".dropbtn");
const dropdown = document.querySelector(".dropdown-content");
// Show/hide dropdown when button is clicked
btn.addEventListener("click", function() {
dropdown.classList.toggle("show");
});
// Search the dropdown when text is entered in the input field
const searchInput = document.getElementById("searchInput");
searchInput.addEventListener("keyup", function(event) {
const filter = event.target.value.toUpperCase();
const items = dropdown.getElementsByTagName("a");
for (let i = 0; i < items.length; i++) {
const txtValue = items[i].textContent || items[i].innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
items[i].style.display = "";
} else {
items[i].style.display = "none";
}
}
});
```
这个下拉框将显示一个输入框和一个按钮。当用户点击按钮时,下拉框中的项目将显示。当用户在输入框中输入文本时,下拉框将根据用户的搜索过滤项目。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![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)