欲實現(xiàn)一個文字搜索的功能,要求輸入時愁憔,鍵盤回車按鈕提示顯示為“搜索”腕扶。效果如下:
開始~
input type=text并不能達到這種效果,google了一下吨掌,html5 增加的type=search可以做到(但需要input type=search外面包上一層帶action屬性的form)半抱。
<div class="search-input-wrap clearfix">
<div class="form-input-wrap f-l">
<form action="" class="input-kw-form">
<input type="search" autocomplete="off" name="baike-search" placeholder="請輸入關(guān)鍵詞" class="input-kw">
</form>
<i class="iconfont if-message"></i>
<i class="iconfont if-close"></i>
</div>
<i class="search-cancel f-l">取消</i>
</div>
但type=search會有許多默認樣式和行為脓恕,這次開發(fā)遇到的有:
-
會默認下拉框顯示搜索歷史記錄;
-
輸入時自動彈出“x”,“x”的樣式在不同手機上窿侈,樣式不同炼幔;
-
IOS 手機(測試時為iphone6 ios10)上輸入框為橢圓形.
但我們希望樣式按照我們自定義的樣式顯示,并且各個手機上能統(tǒng)一史简。
于是幾經(jīng)google乃秀,得到答案:
- 設(shè)置input autocomplete="off"去掉彈出的下拉框;
- 將默認的“x”隱藏掉:
input[type="search"]::-webkit-search-cancel-button{
display: none;
}
- 針對ios 設(shè)置樣式, 去除ios下input 橢圓形:
-webkit-appearance: none;
同時別忘記圆兵,如果提交搜索時想使用ajax跺讯,那么可以阻止表單的默認行為:
container.on('submit', '.input-kw-form', function(event){
event.preventDefault();
})