gpt4 book ai didi

javascript - 按元素名称过滤表

转载 作者:行者123 更新时间:2023-12-02 16:20:30 27 4
gpt4 key购买 nike

我试图通过在搜索表单中写入一些文本来过滤此 HTML 表,这效果很好,但每当过滤时,元素都会尝试填充表的整个宽度,而不是保持其原始元素宽度(25%表格的宽度,不计算单元格之间的空格)。

function searchFilter () {
const input = document.getElementById('myInput');
const filter = input.value.toUpperCase();
const table = document.getElementById('tablaPiola');
const articule = table.getElementsByTagName('td');

for (i = 0; i < articule.length; i++) {
a = articule[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
articule[i].style.display = "";
} else {
articule[i].style.display = "none";
}
}
}

document.getElementById("filter-btn").addEventListener("click", searchFilter);
body {
background-color: black;
}

table
{border-spacing: 20px;
table-layout: fixed;
width: 600px;
margin: auto;
margin-top: 10px;}

td
{text-align: center;
border-radius: 10px;}

td a
{width: 100%;
display: block;
line-height: 50px;
text-decoration: none;
font-family: "Poppins";
font-weight: 700;
font-size: 12px;
color: white;}

.automatizaciones
{background-image: url("https://via.placeholder.com/100/0000FF/0000FF");
background-size: cover;}

.bpm_a_ms
{background-image: url("https://via.placeholder.com/100/0000FF/0000FF");
background-size: cover;}

.compresion
{background-image: url("https://via.placeholder.com/100/0000FF/0000FF");
background-size: cover;}

.compresion_multibanda
{background-image: url("https://via.placeholder.com/100/0000FF/0000FF");
background-size: cover;}
<input type="text" id="myInput">
<input type="button" id="filter-btn" value="Apply">
<table class="tabla_basico" id="tablaPiola">
<tr>
<td class="automatizaciones">
<div class="overlay_basico"><a href="/">AUTOMATIZACIONES</a></div>
</td>
<td class="bpm_a_ms">
<div class="overlay_intermedio"><a href="/">BPM A MS</a></div>
</td>
<td class="compresion">
<div class="overlay_basico"><a href="compresion.html">COMPRESIÓN</a></div>
</td>
<td class="compresion_multibanda">
<div class="overlay_intermedio"><a href="/">COMPRESIÓN MULTIBANDA</a></div>
</td>
</tr>
</table>

This is my webpage

This is how it filters

最佳答案

我最初认为可见性:隐藏,但你指出这意味着显示的内容不再位于左侧。

我唯一的其他想法是在末尾添加填充单元:

function searchFilter () {
const input = document.getElementById('myInput');
const filter = input.value.toUpperCase();
const table = document.getElementById('tablaPiola');
// Remove any fillers we added last time
table.querySelectorAll(".filler").forEach(filler => {
filler.parentNode.removeChild(filler);
});
const articule = table.getElementsByTagName('td');
// Remember how many are showing at the end
let showing = 0;

for (i = 0; i < articule.length; i++) {
a = articule[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
articule[i].style.display = "";
++showing; // Remember we're showing this one
} else {
articule[i].style.display = "none";
}
}
// Grab the length (since `articule` is a live list
const max = articule.length;
// Add blank cells to the end
while (showing < max) {
const filler = document.createElement("td");
filler.className = "filler";
table.appendChild(filler);
++showing;
}
}

实例:

function searchFilter () {
const input = document.getElementById('myInput');
const filter = input.value.toUpperCase();
const table = document.getElementById('tablaPiola');
// Remove any fillers we added last time
table.querySelectorAll(".filler").forEach(filler => {
filler.parentNode.removeChild(filler);
});
const articule = table.getElementsByTagName('td');
// Remember how many are showing at the end
let showing = 0;

for (i = 0; i < articule.length; i++) {
a = articule[i].getElementsByTagName("a")[0];
txtValue = a.textContent || a.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
articule[i].style.display = "";
++showing; // Remember we're showing this one
} else {
articule[i].style.display = "none";
}
}
// Grab the length (since `articule` is a live list
const max = articule.length;
// Add blank cells to the end
while (showing < max) {
const filler = document.createElement("td");
filler.className = "filler";
table.appendChild(filler);
++showing;
}
}

document.getElementById("filter-btn").addEventListener("click", searchFilter);
body {
background-color: black;
}

table
{border-spacing: 20px;
table-layout: fixed;
width: 1200px;
margin: auto;
margin-top: 100px;}

td
{text-align: center;
border-radius: 10px;}

td a
{width: 100%;
display: block;
line-height: 150px;
text-decoration: none;
font-family: "Poppins";
font-weight: 700;
font-size: 17.5px;
color: white;}

.automatizaciones
{background-image: url(imagenes/basico/automatizaciones/articulo.jpg);
background-size: cover;}

.bpm_a_ms
{background-image: url(imagenes/intermedio/bpm_a_ms/articulo.jpg);
background-size: cover;}

.compresion
{background-image: url(imagenes/basico/compresion/articulo.jpg);
background-size: cover;}

.compresion_multibanda
{background-image: url(imagenes/intermedio/compresion_multibanda/articulo.jpg);
background-size: cover;}
<input type="text" id="myInput">
<input type="button" id="filter-btn" value="Apply">
<table class="tabla_basico" id="tablaPiola">
<tr>
<td class="automatizaciones">
<div class="overlay_basico"><a href="/">AUTOMATIZACIONES</a></div>
</td>
<td class="bpm_a_ms">
<div class="overlay_intermedio"><a href="/">BPM A MS</a></div>
</td>
<td class="compresion">
<div class="overlay_basico"><a href="compresion.html">COMPRESIÓN</a></div>
</td>
<td class="compresion_multibanda">
<div class="overlay_intermedio"><a href="/">COMPRESIÓN MULTIBANDA</a></div>
</td>
</tr>
</table>

<小时/>

一些与主要问题没有直接关系的其他注释:

  • 该代码成为我所说的 The Horror of Implicit Globals 的牺牲品。您从不声明 iatxtValue,因此在分配给它们时最终会创建全局变量。我建议使用strict mode在所有代码中,因此当您尝试执行此操作时会收到错误。

  • FWIW,我看到您正在使用 ES2015+ 功能(例如 const),因此您可以使用 for-of 循环而不是 for循环。 for-of 的输入量要少一些。但您可能需要在某些平台上填充可迭代性。我的回答here展示了如何做到这一点。

关于javascript - 按元素名称过滤表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60590907/

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