gpt4 book ai didi

javascript - 如何使 javascript 中的 div 不可见?

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

所以我有下一个代码:

function myFunction() {
var input, filter, ul, li, a, i;
input = document.getElementById("myInput");
filter = input.value.toUpperCase();
ul = document.getElementById("myUL");
li = ul.getElementsByTagName("li");
for (i = 0; i < li.length; i++) {
a = li[i].getElementsByTagName("a")[0];
if (a.innerHTML.toUpperCase().indexOf(filter) > -1) {
li[i].style.display = "";
// div[i].style.display = "";
} else {
li[i].style.display = "none";
//div[i].style.display = "none";
}
}
}
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>WebSite</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<style>
* {
box-sizing: border-box;
}

#networdapp {
display: none;
}

#myInput {
background-image: url('/css/searchicon.png');
background-position: 10px 12px;
background-repeat: no-repeat;
width: 100%;
font-size: 16px;
padding: 12px 20px 12px 40px;
border: 1px solid #ddd;
margin-bottom: 12px;
}

#myUL {
list-style-type: none;
padding: 0;
margin: 0;
}

#myUL li a {
border: 1px solid #ddd;
margin-top: -1px;
/* Prevent double borders */
background-color: #f6f6f6;
padding: 12px;
text-decoration: none;
font-size: 18px;
color: black;
display: block
}

#myUL li a:hover:not(.header) {
background-color: #eee;
}
</style>
</head>

<body>

<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" id="myInput" onkeyup="myFunction()" placeholder="Search for names.." title="Type in a name" type="search" placeholder="Search">
</form>

<div id="test" class="networdapp">
<div class="container" id="test2">
<ul id="myUL">
<div class="container-fluid" id="networdapp">
<div class="row">
<div v-for="result in results" class="col-sm-6">
<div class="card m-3 h-240 bg-light">
<div class="card-header text-center">
<li><a href="#">Adele</a></li>
</div>
<div class="card-body" style="height:200px">
<p class="card-text" v-html="result.prevDesc"></p>
</div>
<div class="card-footer bg-transparent border-info">
<a href="#" class="btn btn-info">Details</a>
</div>
</div>
</div>
</div>
</div>

<div id="test" class="networdapp">
<div class="container" id="test2">
<ul id="myUL">
<div class="container-fluid" id="networdapp">
<div class="row">
<div v-for="result in results" class="col-sm-6">
<div class="card m-3 h-240 bg-light">
<div class="card-header text-center">
<li><a href="#">Agnes</a></li>
</div>
<div class="card-body" style="height:200px">
<p class="card-text" v-html="result.prevDesc"></p>
</div>
<div class="card-footer bg-transparent border-info">
<a href="#" class="btn btn-info">Details</a>
</div>
</div>
</div>
</div>
</div>

<div id="test" class="networdapp">
<div class="container" id="test2">
<ul id="myUL">
<div class="container-fluid" id="networdapp">
<div class="row">
<div v-for="result in results" class="col-sm-6">
<div class="card m-3 h-240 bg-light">
<div class="card-header text-center">
<li><a href="#">Billy</a></li>
</div>
<div class="card-body" style="height:200px">
<p class="card-text" v-html="result.prevDesc"></p>
</div>
<div class="card-footer bg-transparent border-info">
<a href="#" class="btn btn-info">Details</a>
</div>
</div>
</div>
</div>
</div>

<div id="test" class="networdapp">
<div class="container" id="test2">
<ul id="myUL">
<div class="container-fluid" id="networdapp">
<div class="row">
<div v-for="result in results" class="col-sm-6">
<div class="card m-3 h-240 bg-light">
<div class="card-header text-center">
<li><a href="#">Bob</a></li>
</div>
<div class="card-body" style="height:200px">
<p class="card-text" v-html="result.prevDesc"></p>
</div>
<div class="card-footer bg-transparent border-info">
<a href="#" class="btn btn-info">Details</a>
</div>
</div>
</div>
</div>
</div>
</ul>
</div>
</div>
</body>


</html>

我想做的是根据该输入框的输入显示一些 div。到目前为止我所做的是只显示“li”标签,但我真正想要的是显示整个 div 而不是根据输入数据,只有“li”必须显示或不显示 div。上面显示的是我现在的代码。

提前致谢!

最佳答案

当您显示“LI”时,您可以使用“parentElement”显示它的父 DIV。请检查以下代码段。

你的代码

li[i].style.display = "none";
li[i].style.display = "";

新代码

li[i].parentElement.style.display = "none";
li[i].parentElement.style.display = "";

关于javascript - 如何使 javascript 中的 div 不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52204435/

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