[英]How to display all nested list elements according to input text search?
I'm creating a web application that returns an NBA player's name and jersey number when a user searches for a player (and the match is found).我正在创建一个 Web 应用程序,当用户搜索球员(并找到匹配项)时,该应用程序会返回 NBA 球员的姓名和球衣号码。
I can return the name that has been searched, but I cannot return the jersey number.我可以返回搜索到的名字,但无法返回球衣号码。
I've tried setting style.display = true
, which works for the name node, but I can't get this to work on the jersey node.我试过设置style.display = true
,它适用于名称节点,但我无法style.display = true
在球衣节点上工作。
Here's how my HTML has been created with DOM manipulation from a JSON:以下是使用 JSON 中的 DOM 操作创建我的 HTML 的方式:
function searchPlayer() { let input, filter, ul, li, playerName, i; input = document.getElementById("search-bar"); filter = input.value.toUpperCase(); li = document.getElementById("player-list").getElementsByTagName("li"); Object.keys(li).forEach(function(name) { playerName = li[name].innerHTML; if (playerName.toUpperCase().indexOf(filter) > -1) { li[name].style.display = true; } else { li[name].style.display = "none"; } }) }
<div id="player-list-section"> <ul id="player-list"> <li id="player"> <li id="full-name">Alex Abrines</li> <li id="jersey">Jersey: 8</li> </li> <li id="player"> <li id="full-name">Quincy Acy</li> <li id="jersey">Jersey: 13</li> </li> </ul> </div> <input id="search-bar" /> <button onclick="searchPlayer()">Search</button>
I know I can access the child node of player
using eg li[name].childNode[1]
(which returns the jersey li), but I can't call anything on this, such as .innerHTML
or .style.display
.我知道我可以使用例如li[name].childNode[1]
(它返回球衣 li)访问player
的子节点,但我不能调用任何东西,例如.innerHTML
或.style.display
。
How can I return both the name and the jersey?我怎样才能同时退回名字和球衣?
You need to use list-item
instead of true
if you want to show the list items after hidding them using display:none
and use .closest(".player")
to toggle the display of the parent instead:如果要在使用display:none
隐藏列表项后显示列表项并使用.closest(".player")
来切换父项的显示,则需要使用list-item
而不是true
:
if (playerName.toUpperCase().indexOf(filter) > -1) {
li[name].closest(".player").display = 'block';
} else {
li[name].closest(".player").display = "none";
}
NOTE 1: You need to validate your structure li
can't be a parent of another li
, check my updated HTML format.注意 1:您需要验证您的结构li
不能是另一个li
的父级,请检查我更新的 HTML 格式。
NOTE 2: You need also to replace the duplicate id
by common classes since the identifier must be unique in the same document.注意 2:您还需要用公共类替换重复的id
,因为标识符在同一文档中必须是唯一的。
function searchPlayer() { let input, filter, ul, li, playerName, i; input = document.getElementById("search-bar"); filter = input.value.toUpperCase(); li = document.querySelectorAll("#player-list .full-name"); Object.keys(li).forEach(function(name) { playerName = li[name].innerHTML; if (playerName.toUpperCase().indexOf(filter) > -1) { li[name].closest(".player").style.display = 'list-item'; } else { li[name].closest(".player").style.display = "none"; } }) }
<input id="search-bar" oninput='searchPlayer()'> <div id="player-list-section"> <ul id="player-list"> <li class="player"> <ul> <li class="full-name">Alex Abrines</li> <li class="jersey">Jersey: 8</li> </ul> </li> <li class="player"> <ul> <li class="full-name">Quincy Acy</li> <li class="jersey">Jersey: 13</li> </ul> </li> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.