搜索条
搜索条未封装JS文件,要实现示例效果,请参照示例代码中的JS封装。
示例源码
<div class="aui-searchbar" id="search">
<div class="aui-searchbar-input aui-border-radius">
<i class="aui-iconfont aui-icon-search"></i>
<input type="search" placeholder="请输入搜索内容" id="search-input">
<div class="aui-searchbar-clear-btn">
<i class="aui-iconfont aui-icon-close"></i>
</div>
</div>
<div class="aui-searchbar-btn" tapmode>取消</div>
</div>
<section class="aui-content-padded">
更新说明:取消了搜索条的绝对定位。增加清除按钮
</section>
<section class="aui-content-padded">
要搜索的内容为:<span class="aui-text-info" id="search-keywords"></span>
</section>
<script type="text/javascript">
var searchBar = document.querySelector(".aui-searchbar");
var searchBarInput = document.querySelector(".aui-searchbar input");
var searchBarBtn = document.querySelector(".aui-searchbar .aui-searchbar-btn");
var searchBarClearBtn = document.querySelector(".aui-searchbar .aui-searchbar-clear-btn");
if(searchBar){
searchBarInput.onclick = function(){
searchBarBtn.style.marginRight = 0;
}
searchBarInput.oninput = function(){
if(this.value.length){
searchBarClearBtn.style.display = 'block';
searchBarBtn.classList.add("aui-text-info");
searchBarBtn.textContent = "搜索";
}else{
searchBarClearBtn.style.display = 'none';
searchBarBtn.classList.remove("aui-text-info");
searchBarBtn.textContent = "取消";
}
}
}
searchBarClearBtn.onclick = function(){
this.style.display = 'none';
searchBarInput.value = '';
searchBarBtn.classList.remove("aui-text-info");
searchBarBtn.textContent = "取消";
}
searchBarBtn.onclick = function(){
var keywords = searchBarInput.value;
if(keywords.length){
searchBarInput.blur();
document.getElementById("search-keywords").textContent = keywords;
}else{
this.style.marginRight = "-"+this.offsetWidth+"px";
searchBarInput.value = '';
searchBarInput.blur();
}
}
</script>