Como criar: menu de busca
- Página anterior Barra de navegação com ícone
- Próxima página Barra de pesquisa
Aprenda a usar JavaScript para criar um menu de busca que filtre links.
Buscar/Filtrar menu
Como buscar links no menu de navegação:
Conteúdo da Página
Por favor, insira uma categoria/ligação específica na barra de pesquisa para "filtrar" as opções de busca.
Algum texto...Algum texto...Algum texto...Algum texto...Algum texto...Algum texto...Algum texto...Algum texto...
Algum outro texto...Algum texto...Algum texto...Algum texto...Algum texto...Algum texto...Algum texto...Algum texto...
Algum texto...
Criar menu de busca
Primeiro passo - Adicionar HTML:
<input type="text" id="mySearch" onkeyup="myFunction()" placeholder="Search.." title="Type in a category"> <ul id="myMenu"> <li><a href="#">HTML</a></li> <li><a href="#">CSS</a></li> <li><a href="#">JavaScript</a></li> <li><a href="#">PHP</a></li> <li><a href="#">Python</a></li> <li><a href="#">jQuery</a></li> <li><a href="#">SQL</a></li> <li><a href="#">Bootstrap</a></li> <li><a href="#">Node.js</a></li> </ul>
Note:In this demonstration, we use href="#"
Since we do not have any linkable pages. In actual practice, this should be a real URL pointing to a specific page.
Second step - Add CSS:
Set the style of the search box and navigation menu:
/* Set the style of search box */ #mySearch { width: 100%; font-size: 18px; padding: 11px; border: 1px solid #ddd; } /* Set the style of navigation menu */ #myMenu { list-style-type: none; padding: 0; margin: 0; } /* Set the style of navigation links */ #myMenu li a { padding: 12px; text-decoration: none; color: black; display: block } #myMenu li a:hover { background-color: #eee; }
Third step - Add JavaScript:
<script> function myFunction() { // Declare variables var input, filter, ul, li, a, i; input = document.getElementById("mySearch"); filter = input.value.toUpperCase(); ul = document.getElementById("myMenu"); li = ul.getElementsByTagName("li"); // Loop through all list items and hide those that do not match the search query for (i = 0; i < li.length; i++) { a = li[i].getElementsByTagName("a")[0]; if (a.innerHTML.toUpperCase().indexOf(filter) > -1) { li[i].style.display = ""; } else { li[i].style.display = "none"; } } } </script>
Tip:If you want to perform a case-sensitive search, please remove toUpperCase()
.
Related pages
Tutorial:How to filter a table
Tutorial:Como filtrar a lista
- Página anterior Barra de navegação com ícone
- Próxima página Barra de pesquisa