js實現(xiàn)帶菜單欄目搜索的下拉菜單
更新時間:2022年12月22日 15:10:42 投稿:yin
在下拉菜單欄目比較多的時候,用戶查找欄目閱讀會很麻煩,為了提高用戶體驗度,加個欄目搜索會很方便。這篇文章主要介紹了js實現(xiàn)帶菜單欄目搜索的下拉菜單,感覺挺實用的,需要的朋友可以參考下
在下拉菜單欄目比較多的時候,用戶查找欄目閱讀會很麻煩,為了提高用戶體驗度,加個欄目搜索會很方便。這篇文章主要介紹了js實現(xiàn)帶菜單欄目搜索的下拉菜單,感覺挺實用的,需要的朋友可以參考下
js實現(xiàn)帶搜索的下拉菜單 — 基礎(chǔ) HTML 代碼
<div class="dropdown"> <button onclick="myFunction()" class="dropbtn">下拉菜單</button> <div id="myDropdown" class="dropdown-content"> <input type="text" placeholder="Search.." id="myInput" onkeyup="filterFunction()"> <a href="#about" rel="external nofollow" >Google</a> <a href="#base" rel="external nofollow" >Runoob</a> <a href="#blog" rel="external nofollow" >Taobao</a> <a href="#contact" rel="external nofollow" >Wiki</a> <a href="#custom" rel="external nofollow" >Zhihu</a> <a href="#support" rel="external nofollow" >Tmall</a> <a href="#tools" rel="external nofollow" >Weibo</a> </div> </div>
js實現(xiàn)帶搜索的下拉菜單 — 菜單的樣式:
/* 下拉菜單按鈕 */ .dropbtn { background-color: #04AA6D; color: white; padding: 16px; font-size: 16px; border: none; cursor: pointer; } /* 鼠標(biāo)移動到下拉菜單按鈕到樣式*/ .dropbtn:hover, .dropbtn:focus { background-color: #3e8e41; } /* 搜索框 */ #myInput { box-sizing: border-box; background-image: url('searchicon.png'); background-position: 14px 12px; background-repeat: no-repeat; font-size: 16px; padding: 14px 20px 12px 45px; border: none; border-bottom: 1px solid #ddd; } /* 搜索框獲取焦點的樣式 */ #myInput:focus {outline: 3px solid #ddd;} /* 容器 <div> - 定位下拉菜單 */ .dropdown { position: relative; display: inline-block; } /* 下拉菜單內(nèi)容 (默認(rèn)隱藏) */ .dropdown-content { display: none; position: absolute; background-color: #f6f6f6; min-width: 230px; border: 1px solid #ddd; z-index: 1; } /* 下拉菜單鏈接樣式 */ .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } /* 鼠標(biāo)移動到鏈接上的樣式 */ .dropdown-content a:hover {background-color: #f1f1f1} /* 顯示下拉菜單 (使用 JS 添加 .dropdown-content 類) */ .show {display:block;}
js實現(xiàn)帶搜索的下拉菜單 — JavaScript 代碼:
/* 點擊按鈕設(shè)置下拉菜單的顯示與隱藏 */ function myFunction() { document.getElementById("myDropdown").classList.toggle("show"); } /* 搜索功能 */ function filterFunction() { var input, filter, ul, li, a, i; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); div = document.getElementById("myDropdown"); a = div.getElementsByTagName("a"); for (i = 0; i < a.length; i++) { txtValue = a[i].textContent || a[i].innerText; if (txtValue.toUpperCase().indexOf(filter) > -1) { a[i].style.display = ""; } else { a[i].style.display = "none"; } } }
到此這篇關(guān)于js實現(xiàn)帶菜單欄目搜索的下拉菜單的文章就介紹到這了,更多相關(guān)js實現(xiàn)帶搜索的下拉菜單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用JTrackBar實現(xiàn)的模擬蘋果風(fēng)格的滾動條
用JTrackBar實現(xiàn)的模擬蘋果風(fēng)格的滾動條...2007-08-08