gpt4 book ai didi

JavaScript:搜索功能,使用搜索栏输入去除无序列表元素

转载 作者:行者123 更新时间:2023-11-28 14:01:49 25 4
gpt4 key购买 nike

我有一个带有 div 的 div.header。使用我的调用函数搜索我的输入标签所在的位置。我还有包含要搜索的所有元素的 div.content。它们位于不同的 div 标签中,但我的 JavaScript 的调用函数仅在 input 标签中。问题是当我输入输入标签时,我的控制台告诉我:

(21) Champions.htm:564 未捕获类型错误:li.getElementsByClassName 不是函数 在搜索功能 (Champions.htm:564) 在 HTMLInputElement.onkeyup (Champions.htm:39)

我正在寻找的解决方案是如何在输入特定冠军名称时让我的输入标签删除搜索元素。示例:

在输入标签中输入 A 应该会删除所有带有 h3 标签但不符合搜索条件的元素。

注意:我的h3标签填的是具体的名字。如果这些名称不符合搜索条件,searchfunction() 应该查找并从页面中删除这些名称。

我不是 JavaScript 的忠实粉丝,所以我的大部分尝试都是徒劳的。

            function searchfunction() {
// Definiton af variabler
// Defining my variables
var input, filter, div1, div2, ul, li, element, h3, a, i, txtValue;
input = document.getElementById('s-input');
filter = input.value.toUpperCase();
ul = document.getElementById("s-output");
li = ul.getElementsByTagName('li');
element = li.getElementsByClassName('s-element');
h3 = element.getElementsByTagName('h3');

// Loop igennem alle elementer og gem dem som ikke matcher søgekriteriet
// Loop through all elements and hide the ones that doesn't match the criteria
for (i = 0; i < h3.length; i++) {
a = h3[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
    <!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta name="viewport" content="width=device-width, initial-scale=1" charset="utf-8">
<title>Basement Of Legends</title>
<link rel="stylesheet" href="style2.css">
<link rel="icon"
href="https://mmls.mmu.edu.my/wordpress/1161100407/wp-content/uploads/sites/33888/2017/01/052b0714815331.562895156726a.png">
<!-- Font libraries -->
<link href="https://fonts.googleapis.com/css?family=Arvo|Cinzel|Staatliches&display=swap" rel="stylesheet">
</head>

<body>
<main id="website">
<!-- Sidebar -->
<div class="sidebar">
<a class="linkbox" href="C:\Users\emils\OneDrive\Dokumenter\leaguesite\Frontpage.htm">
<div title="Go to frontpage" class="sitelinks">
<p class="linktext">Frontpage</p>
</div>
</a>
<a class="linkbox" href="C:\Users\emils\OneDrive\Dokumenter\leaguesite\Champions.htm">
<div title="Go to champions" class="sitelinks">
<p class="linktext">Champions</p>
</div>
</a>
<a class="linkbox" href="C:\Users\emils\OneDrive\Dokumenter\leaguesite\FAQ.htm">
<div title="Go to F.A.Q." class="sitelinks">
<p class="linktext">F.A.Q.</p>
</div>
</a>
</div>
<div class="container">
<!-- Website Head -->
<div class="header">
<div class="search">
<input title="Search for a specific element" onkeyup="searchfunction()" type="text" id="s-input"
placeholder="Search for elements..">
</div>
</div>
<!-- Content -->
<div class="content">
<div class="searchcontent">
<ul id="s-output">
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/aatrox-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Aatrox</h3>
<h5>the World Ender</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/ahri-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Ahri</h3>
<h5>the Nine-Tailed Fox</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/champion/square/akali.png"
alt="champion.png">
<div class="Profile">
<h3>Akali</h3>
<h5>the Rogue Assassin</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/alistar-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Alistar</h3>
<h5>the Minotaur</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/amumu-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Amumu</h3>
<h5>the Sad Mummy</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/anivia-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Anivia</h3>
<h5>the Cryophoenix</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/annie-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Annie</h3>
<h5>the Dark Child</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/ashe-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Ashe</h3>
<h5>the Frost Archer</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/aurelion-sol-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Aurelion Sol</h3>
<h5>the Star Forger</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/azir-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Azir</h3>
<h5>the Emperor of the Sands</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/bard-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Bard</h3>
<h5>the Wandering Caretaker</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/blitzcrank-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Blitzcrank</h3>
<h5>the Great Steam Golem</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/brand-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Brand</h3>
<h5>the Burning Vengeance</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/braum-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Braum</h3>
<h5>the Heart of the Freljord</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/caitlyn-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Caitlyn</h3>
<h5>the Sheriff of Piltover</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/camille-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Camille</h3>
<h5>the Steel Shadow</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/cassiopeia-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Cassiopeia</h3>
<h5>the Serpent's Embrace</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/chogath-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Cho'gath</h3>
<h5>the Terror of the Void</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/corki-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Corki</h3>
<h5>the Daring Bombardier</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/darius-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Darius</h3>
<h5>the Hand of Noxus</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/diana-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Diana</h3>
<h5>Scorn of the Moon</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/dr-mundo-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Dr. Mundo</h3>
<h5>the Madman of Zaun</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/draven-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Draven</h3>
<h5>the Glorius Executioner</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/ekko-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Ekko</h3>
<h5>the Boy Who Shattered Time</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>

</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/elise-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Elise</h3>
<h5>the Spider Queen</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/evelynn-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Evelynn</h3>
<h5>Agony's Embrace</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/ezreal-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Ezreal</h3>
<h5>the Prodigal Explorer</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/fiddlesticks-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Fiddlesticks</h3>
<h5>the Prodigal Explorer</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/fiora-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Fiora</h3>
<h5>the Prodigal Explorer</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/fizz-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Fizz</h3>
<h5>the Prodigal Explorer</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/galio-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Galio</h3>
<h5>the Prodigal Explorer</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/gangplank-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Gangplank</h3>
<h5>the Prodigal Explorer</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/garen-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Garen</h3>
<h5>the Prodigal Explorer</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
</li>
<li>
<div class="s-element">
<img class="pb" src="https://www.mobafire.com/images/avatars/gnar-classic.png"
alt="champion.png">
<div class="Profile">
<h3>Gnar</h3>
<h5>the Prodigal Explorer</h5>
</div>
<p>
Description: hes O.D.
</p>
</div>
</li>

</ul>

</div>
<!-- Content bund -->
<footer>
<ul class="footinfo">
<li><a class="foottext" target="_blank" href="">| Privacy Policy</a></li>
<li><a class="foottext" target="_blank" href="">| Sponsors</a></li>
<li><a class="foottext" target="_blank" href="">| e.t.c.</a></li>
</ul>
</footer>
</div>
</div>
<script>
function searchfunction() {
// Definiton af variabler
// Defining my variables
var input, filter, div1, div2, ul, li, element, h3, a, i, txtValue;
input = document.getElementById('s-input');
filter = input.value.toUpperCase();
ul = document.getElementById("s-output");
li = ul.getElementsByTagName('li');
element = li.getElementsByClassName('s-element');
h3 = element.getElementsByTagName('h3');

// Loop igennem alle elementer og gem dem som ikke matcher søgekriteriet
// Loop through all elements and hide the ones that doesn't match the criteria
for (i = 0; i < h3.length; i++) {
a = h3[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
} else {
li[i].style.display = "none";
}
}
}
</script>
</body>

</html>

最佳答案

li is an array as getElementsByTagName will return array. You have to either iterate through it or use index in order to use getElementsByClassName - Mohan Ramalingham

同时为了使您的代码更具可读性,请仅选择必要的元素。jsfiddle 链接:jsfiddle example

var items = document.querySelectorAll('#s-output li');
var profile = document.querySelectorAll('.Profile h3');
var searchbar = document.querySelector('.search input');

//Append the eventlistener
searchbar.addEventListener('keyup', searchfunction, false);

function searchfunction(_) {
//Value of the searchbar
var searchbarKeys = _.target.value.toLowerCase();

//Check if the title matches the current value of the searchbar
items.forEach(function(item, index) {
//toLowerCase to make it case insensitive (optional)
var title = profile[index].innerText.toLowerCase()

if (title.indexOf(searchbarKeys) > -1) {
item.style.display = 'block';
} else {
item.style.display = 'none';
}
});
}

关于JavaScript:搜索功能,使用搜索栏输入去除无序列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57604954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com