gpt4 book ai didi

javascript - 在导航中获取已过滤的行数

转载 作者:行者123 更新时间:2023-12-03 01:45:24 25 4
gpt4 key购买 nike

当我导航时,我试图从 html 表中获取已过滤的行数。我找不到任何明确的解决方案,请问最好的方法是什么?

这是脚本的过滤部分:

function searchPokemon() {

var input, filter, found, table, tr, td, i, j;
input = document.getElementById("mySearch");
filter = input.value.toUpperCase();
table = document.getElementById("pokemons-list");
tr = table.getElementsByTagName("tr");

for (i = 0; i < tr.length; i++) {

td = tr[i].getElementsByTagName("td");

for (j = 0; j < td.length; j++) {

if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {

found = true;
}
}

if (found) {

tr[i].style.display = "";
found = false;
} else {

tr[i].style.display = "none";
}
}
}

这里是表格导航部分:

var rows = document.getElementById("pokemons-list").children[1].children;
var selectedRow = 0;

document.body.onkeydown = function(e){

rows[selectedRow].style.backgroundColor = "#FFFFFF";

if(e.keyCode == 38){
selectedRow--;
} else if(e.keyCode == 40){
selectedRow++;
} else if(e.keyCode == 13){
Pokemon_ID = selectedRow + 1;
alert('Pokemon_ID = ' + Pokemon_ID);
}
if(selectedRow >= rows.length){
selectedRow = 0;
} else if(selectedRow < 0){
selectedRow = rows.length-1;
}

rows[selectedRow].style.backgroundColor = "dodgerblue";

rows[selectedRow].scrollIntoView(true);

};

最佳答案

下面的代码片段是您正在尝试执行的操作的基本示例。运行代码片段并输入“Aa”(不带引号)。请注意,当您按向下箭头时,您会立即获得第一个 Aa,但随后必须再按 3 次才能突出显示下一行。下面的两个向下箭头实际上位于隐藏的 tr 元素上。这些元素被隐藏起来,无法向用户显示,但它们仍然位于 DOM 本身内。

解决方案:在 onkeydown 事件期间,在将背景颜色更改为 dodgerblue 之前,请验证当前 tr 元素没有 display none。如果是,则再次循环并检查下一个预期结果。既然你已经循环了,只要你不循环回到你开始的地方,那么你就不应该有无限循环。

var searchPokemon;
var rows = document.getElementById("pokemons-list").children[1].children;
var selectedRow = 0;

document.body.onkeydown = function(e) {
rows[selectedRow].style.backgroundColor = "#FFFFFF";

if(e.keyCode == 38){
selectedRow--;
} else if(e.keyCode == 40){
selectedRow++;
} else if(e.keyCode == 13){
Pokemon_ID = selectedRow + 1;
alert('Pokemon_ID = ' + Pokemon_ID);
}

if(selectedRow >= rows.length){
selectedRow = 0;
} else if(selectedRow < 0){
selectedRow = rows.length-1;
}

rows[selectedRow].style.backgroundColor = "dodgerblue";

rows[selectedRow].scrollIntoView(true);

};

searchPokemon = function() {

var input, filter, found, table, tr, td, i, j;
input = document.getElementById("mySearch");
filter = input.value.toUpperCase();
table = document.getElementById("pokemons-list");
tr = table.getElementsByTagName("tr");

for (i = 0; i < tr.length; i++) {

td = tr[i].getElementsByTagName("td");

for (j = 0; j < td.length; j++) {

if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {

found = true;
}
}

if (found) {

tr[i].style.display = "";
found = false;
} else {

tr[i].style.display = "none";
}
}
}
<html>
<head>
</head>
<body>
<input id="mySearch" />
<button type="button" onclick="searchPokemon()">Filter</button>
<table id="pokemons-list">
<thead> </thead>
<tbody>
<tr style="background-color: rgb(255, 255, 255);">
<td>1</td>
<td>Aaa</td><td>
<img src=""></td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>2</td>
<td>Aba</td><td>
<img src=""></td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>3</td>
<td>Abb</td><td>
<img src=""></td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>4</td>
<td>Aab</td><td>
<img src=""></td>
</tr>
</tbody>
</table>
</body>
</html>

下面的代码片段应该更新,以便您可以进行相同的测试,但跳过隐藏的 tr 元素。

var searchPokemon;
var rows = document.getElementById("pokemons-list").children[1].children;
var selectedRow = 0;

document.body.onkeydown = function (e) {
rows[selectedRow].style.backgroundColor = "#FFFFFF";

var startedAt;

do {
startedAt = selectedRow;

if (e.keyCode == 38) {
selectedRow--;
} else if (e.keyCode == 40) {
selectedRow++;
} else if (e.keyCode == 13) {
Pokemon_ID = selectedRow + 1;
alert('Pokemon_ID = ' + Pokemon_ID);
}

if (selectedRow >= rows.length) {
selectedRow = 0;
} else if (selectedRow < 0) {
selectedRow = rows.length - 1;
}

} while (startedAt != selectedRow && rows[selectedRow].style.display === 'none');

rows[selectedRow].style.backgroundColor = "dodgerblue";
};

searchPokemon = function () {
var input, filter, found, table, tr, td, i, j;
input = document.getElementById("mySearch");
filter = input.value.toUpperCase();
table = document.getElementById("pokemons-list");
tr = table.getElementsByTagName("tr");

for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName("td");

for (j = 0; j < td.length; j++) {
if (td[j].innerHTML.toUpperCase().indexOf(filter) > -1) {
found = true;
}
}

if (found) {
tr[i].style.display = "";
found = false;
} else {
tr[i].style.display = "none";
}
}
}
<html>
<head>
</head>
<body>
<input id="mySearch" />
<button type="button" onclick="searchPokemon()">Filter</button>
<table id="pokemons-list">
<thead> </thead>
<tbody>
<tr style="background-color: rgb(255, 255, 255);">
<td>1</td>
<td>Aaa</td><td>
<img src=""></td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>2</td>
<td>Aba</td><td>
<img src=""></td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>3</td>
<td>Abb</td><td>
<img src=""></td>
</tr>
<tr style="background-color: rgb(255, 255, 255);">
<td>4</td>
<td>Aab</td><td>
<img src=""></td>
</tr>
</tbody>
</table>
</body>
</html>

关于javascript - 在导航中获取已过滤的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50661848/

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