简体   繁体   English

如何在JavaScript中应用来自多个div元素的过滤

[英]How to apply filtering from multiple div element in JavaScript

I'm trying to learn javascript filtering. 我正在尝试学习javascript过滤。 But there are some good examples for filtering from the list elements of the page, but how can I filter an entire div based on the search input? 但是,有一些很好的示例可以从页面的列表元素进行过滤,但是如何根据搜索输入过滤整个div

This is so far I have done, but I'm stuck and not sure how to proceed further, I hope you'll be kind enough to help me learn the process, thanks in advance. 到目前为止,我已经完成了这项工作,但是我仍然感到困惑,并且不确定如何继续进行下去,希望您能有所帮助,以帮助我学习该过程,在此先感谢您。

This is the fiddle and the snippet is given below: 这是小提琴 ,其摘要如下所示:

 const searchBar = document.forms['search-books'].querySelector('input'); searchBar.addEventListener('keyup', function(e) { const term = e.target.value.toLocaleLowerCase(); const books = list.getElementsByTagName('h5'); Array.from(books).forEach(function(books) { const title = book.firstElementChild.textContent; if (title.toLowerCase().indexOf(term) != -1) { book.style.display = 'block'; } else { book.style.display = 'none'; } }) }) 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <form id="search-books"> <input type="text" placeholder="Search a book ... "> </form> <div class="container"> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1447303603s/2767052.jpg"/></div> <div class="col-10"> <h5> The Hunger Games</h5> <a href="The-Hunger-Games.html">Learn</a> </div> </div> <br> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1507396732s/2.jpg"/></div> <div class="col-10"> <h5>Harry Potter</h5> <a href="Harry-Potter.html">Learn</a> </div> </div> </div> 

You need to either declare the list element or get all the elements with Tag h5 . 您需要声明list元素或使用Tag h5获取所有元素。

Since you want to hide or display the element, you should target the parent of the parent of the div with the given text. 由于要隐藏或显示元素,因此应使用给定的文本作为div的父对象的父对象。

 const searchBar = document.forms['search-books'].querySelector('input'); searchBar.addEventListener('keyup', function(e) { const term = e.target.value.toLocaleLowerCase(); const books = document.getElementsByTagName('h5'); Array.from(books).forEach(function(book) { const title = book.textContent; if (title.toLowerCase().indexOf(term) != -1) { book.parentElement.parentElement.style.display = 'flex'; } else { book.parentElement.parentElement.style.display = 'none'; } }) }) 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <form id="search-books"> <input type="text" placeholder="Search a book ... "> </form> <div class="container"> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1447303603s/2767052.jpg"/></div> <div class="col-10"> <h5> The Hunger Games</h5> <a href="The-Hunger-Games.html">Learn</a> </div> </div> <br> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1507396732s/2.jpg"/></div> <div class="col-10"> <h5>Harry Potter</h5> <a href="Harry-Potter.html">Learn</a> </div> </div> </div> 

You can use document.querySelectorAll('.container h5') it will return array all h5 tag inside of container . 您可以使用document.querySelectorAll('.container h5')返回container内部的所有h5标签数组。

DEMO 演示

 const searchBar = document.forms['search-books'].querySelector('input'), books = document.querySelectorAll('.container h5'); searchBar.addEventListener('keyup', function(e) { let term = e.target.value.toLocaleLowerCase(); books.forEach(el => { let title = el.innerText; if (title.toLowerCase().indexOf(term) != -1) { el.parentElement.parentElement.style.display = 'block'; } else { el.parentElement.parentElement.style.display = 'none'; } }) }) 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" /> <form id="search-books"> <input type="text" placeholder="Search a book ... "> </form> <div class="container"> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1447303603s/2767052.jpg" /></div> <div class="col-10"> <h5> The Hunger Games</h5> <a href="The-Hunger-Games.html">Learn</a> </div> </div> <br> <div class="row list-single"> <div class="col-2"><img src="https://images.gr-assets.com/books/1507396732s/2.jpg" /></div> <div class="col-10"> <h5>Harry Potter</h5> <a href="Harry-Potter.html">Learn</a> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM