gpt4 book ai didi

通过过滤器隐藏元素的javascript

转载 作者:行者123 更新时间:2023-11-30 15:47:01 26 4
gpt4 key购买 nike

我有一个 html 页面,其中我具有以下结构:

    <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Search for names..">
<div class="tabella">
<div class="rigaHead"></div>

<div class="rigaEven">
<div class="col1">
<p class='room'>XXX</p>
<p class='where'>WHERE</p>
</div>
<div class="col2 half">
<p class='when'>09:00 - 13:00</p>
</div>
<div class="col3">
<p class="what">YYY</p>
<p class="who">PROFESSOR</p>
</div>
</div>

<div class="rigaOdd">
<div class="col1">
<p class='room'>XXX</p>
<p class='where'>WHERE</p>
</div>
<div class="col2 half">
<p class='when'>09:00 - 13:00</p>
</div>
<div class="col3">
<p class="what">YYY</p>
<p class="who">PROFESSOR</p>
</div>
</div>

</div>
</div>

我只想显示类为 RigaEven 或 RigaOdd 的 div 标签谁的标签在图像中突出显示为与搜索字段的那部分相同的文本... enter image description here我该怎么办?

最佳答案

你可以这样做:

function search(what){
var elements = document.querySelectorAll(".rigaEven, .rigaOdd");
for(var i = 0; i < elements.length;i++){
var v = elements[i];
if(what.length == 0 || v.querySelector(".what").textContent.indexOf(what) != -1){
v.style.display = "block";
}else{
v.style.display = "none";
}
}
}
<div><input type="text" placeholder="Search what" onkeyup="search(this.value)"/></div>
<div class="rigaEven">
<div class="col1">
<p class='room'>XXX</p>
<p class='where'>WHERE</p>
</div>
<div class="col2 half">
<p class='when'>09:00 - 13:00</p>
</div>
<div class="col3">
<p class="what">XXX</p>
<p class="who">PROFESSOR</p>
</div>
</div>

<div class="rigaOdd">
<div class="col1">
<p class='room'>XXX</p>
<p class='where'>WHERE</p>
</div>
<div class="col2 half">
<p class='when'>09:00 - 13:00</p>
</div>
<div class="col3">
<p class="what">YYY</p>
<p class="who">PROFESSOR</p>
</div>
</div>

要使搜索不区分大小写,您可以使用 v.querySelector(".what").textContent.toLowerCase().indexOf(what.toLowerCase())

关于通过过滤器隐藏元素的javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39915752/

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