mui中原生js实现搜索功能
时间: 2024-01-13 11:03:40 浏览: 113
mui.js实现分页功能
好的,以下是一个使用MUI框架实现搜索功能的示例:
HTML代码:
```html
<header class="mui-bar mui-bar-nav">
<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
<h1 class="mui-title">Search</h1>
</header>
<div class="mui-content">
<form class="mui-input-group">
<div class="mui-input-row">
<input type="search" id="searchInput" class="mui-input-clear" placeholder="Search...">
</div>
</form>
<ul class="mui-table-view">
<li class="mui-table-view-cell">Apple</li>
<li class="mui-table-view-cell">Banana</li>
<li class="mui-table-view-cell">Cherry</li>
<li class="mui-table-view-cell">Dragonfruit</li>
<li class="mui-table-view-cell">Elderberry</li>
<li class="mui-table-view-cell">Fig</li>
</ul>
</div>
```
JS代码:
```javascript
mui.init();
const searchInput = document.getElementById('searchInput');
const list = document.querySelector('.mui-table-view');
const items = list.getElementsByTagName('li');
searchInput.addEventListener('input', function () {
const searchValue = this.value.toLowerCase();
for (let i = 0; i < items.length; i++) {
const item = items[i];
const text = item.textContent.toLowerCase();
if (text.indexOf(searchValue) === -1) {
item.classList.add('mui-hidden');
} else {
item.classList.remove('mui-hidden');
}
}
});
```
这段代码同样实现了一个简单的搜索功能,在MUI框架下使用了MUI的样式类来控制显示或隐藏。其中,`mui.init()`是MUI的初始化方法,`input`事件监听用户在输入框中输入内容,`classList`属性用于添加或移除样式类。
阅读全文