- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我进行了很多搜索以找到解决方案,但没有找到明确的解释。我实际上有一个表(使用 PHP 列出来自 mySQL 数据库的数据)并且我希望它可以使用键盘箭头进行导航:当用户按下向上或向下箭头时,它会集中在上方/上方的行并且背景颜色会发生变化。我想学习如何在 JavaScript 中执行此操作,而不是 jQuery 解决方案
这是表格:
<table id="pokemons-list">
<thead>
<tr>
<th>NO</th>
<th>Nom</th>
<th></th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM pokemons ORDER BY id ASC';
foreach ($pdo->query($sql) as $row) {
echo '<tr tabindex="-1">';
echo '<td style="text-align:center">'. $row['id'] . '</td>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. '<img style="vertical-align:middle;margin: 0px 0px 0px -20px;" src="icons/'. $row['id'].'.png"></td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
该表还有一个搜索栏,可以使用以下脚本过滤行:
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";
}
}
}
最佳答案
尝试使用它作为你的 JS:
var rows = document.getElementById("pokemons-list").children[1].children;
var selectedRow = 0;
document.body.onkeydown = function(e){
//Prevent page scrolling on keypress
e.preventDefault();
//Clear out old row's color
rows[selectedRow].style.backgroundColor = "#FFFFFF";
//Calculate new row
if(e.keyCode == 38){
selectedRow--;
} else if(e.keyCode == 40){
selectedRow++;
}
if(selectedRow >= rows.length){
selectedRow = 0;
} else if(selectedRow < 0){
selectedRow = rows.length-1;
}
//Set new row's color
rows[selectedRow].style.backgroundColor = "#FFFFAA";
};
我在一个更基本的表格上做了一个简单的例子:
var rows = document.getElementById("pokemons-list").children[1].children;
var selectedRow = 0;
document.body.onkeydown = function(e){
//Prevent page scrolling on keypress
e.preventDefault();
//Clear out old row's color
rows[selectedRow].style.backgroundColor = "#FFFFFF";
//Calculate new row
if(e.keyCode == 38){
selectedRow--;
} else if(e.keyCode == 40){
selectedRow++;
}
if(selectedRow >= rows.length){
selectedRow = 0;
} else if(selectedRow < 0){
selectedRow = rows.length-1;
}
//Set new row's color
rows[selectedRow].style.backgroundColor = "#8888FF";
};
//Set the first row to selected color
rows[0].style.backgroundColor = "#8888FF";
th, td {
border: 1px solid black;
}
<table id="pokemons-list">
<thead>
<tr>
<th>NO</th>
<th>Nom</th>
</tr>
</thead>
<tbody>
<!--In your case this is auto-generated-->
<tr>
<td>1</td><td>Charmander</td>
</tr>
<tr>
<td>2</td><td>Squirtle</td>
</tr>
<tr>
<td>3</td><td>Bulbasaur</td>
</tr>
<tr>
<td>4</td><td>Pikachu</td>
</tr>
<tr>
<td>5</td><td>Arceus</td>
</tr>
</tbody>
</table>
关于javascript - 使用键盘向上/向下箭头导航表 <tr>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50120063/
我想要数组中所选 tr 的所有下一个 tr 的 data-id。我使用此代码,但这不是一个好代码。 var ids = Array.prototype.slice.call($("tr.selecte
JSFiddle:http://jsfiddle.net/9u8tnh97/ 我正在使用 jQuery 和 Bootstrap。我有一张 table 4 像这样的元素:
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
在 Notepad++ 中,我试图制作一个正则表达式来匹配标签。起初,我认为必须转义大括号,所以我尝试了 \ .然而,这不仅符合我预期的开始标签,而且匹配所有 。 s( 和 )。这是为什么? 最佳答
我正在尝试定位其中包含子表行元素的表中的最后一个父表行。我已尝试使用下面的 jQuery 来定位 :last 伪值,但是,如预期的那样,它的目标是目标父表中的绝对最后一个表行元素。 $('table[
我想插入新的在每个现有的 之后。我的功能: var element = document.querySelector('tr.cart-item'); var newElement = docume
这个问题在这里已经有了答案: RegEx match open tags except XHTML self-contained tags (35 个答案) 关闭 9 年前。 我希望这个正则表达式匹
生成此代码是为了做两件事。第一个是单击复选框时突出显示表记录。第二个是即使页面刷新也能记住结果。 Untitled Document
嗨,我正在尝试在 tr 数组中查找 tr属性名称是 docId 是否有任何类型的 jquery 选择器? 我不想只用 for 循环遍历数组然后自己去找。 除非你说我别无选择... 谢谢 最佳答案 如果
我有一张 table ,我想在单击加号图标时在 tr 中打开相同的 tr。我不明白我使用哪个 html 元素打开。 请帮帮我。提前致谢。
对你来说是个小问题:-) 我正在使用 BeautifulSoup 来解析 HTML 页面中表格的内容。问题是在我的输出文件的每一行(CSV/EXCEL)之间,它拉出一个空行......这是 HTML
我制作了一个包含基本信息的表格,每个奇数行和偶数行都有不同的颜色。在 :hover影响它是橙色的,不管它是偶数还是奇数。 但是当我想“分组”3 我发现了一个问题。我试着把它包装成 但它没有用。我的目标
在 lxml 中,我使用 xpath 选择表(具有不同行数)中的所有 tr,除了包含乱码的最后两行。 是否有排除最后两行的模式匹配?我正在浏览 xpath 教程,显然有一个“except”运算符和一个
我在表列表中显示来自数据库的记录。此外,我在每一行中都有一个跟进按钮,如果任何用户单击跟进按钮,则会为特定用户打开一个包含详细信息的弹出窗口。 或者任何其他想法来处理这个问题? 我试过用这样的东西
我有以下表格结构。 当我点击 时我想添加新的 在 旁边其中被点击。 所以结果会是:
使用这个 HTML: 1 2 3 4 我得到这个输出: 1 2 3 4 但我想要的是: 1 2 3 4 如何
嗨,我有一个表格,我在这里做了一些代码来将表格更改为可编辑和可选择...我的要求是 1.当点击一个TR时,它应该变成蓝色,再次点击另一个TR,它将显示蓝色,旧的TR变成白色(威胁不可选择) 我由于一些
这个问题可能令人困惑,但这就是确切的情况.. 我已经动态生成了一些(根据从数据库获取的数据),现在我想允许用户选择其中一个单选按钮,并且我想捕获所单击的行的详细信息,因此请检查我的代码并协助 我的aj
免责声明:HTML、JQuery、Ajax 技能水平 — 垃圾。一直是一个厚客户。 我有一个表单,允许用户输入客户代码和电子邮件地址。我想在客户代码有效时显示客户的姓名。我会错误地通过 Ajax 和
正在尝试添加 其他元素 表中的元素根据 的行跨度该特定元素 元素。在同一功能中,单击 中的此按钮后元素的行数每次增加1,因此也需要增加行数。不知道该怎么做。也许使用 slice()以某种方式发挥作
我是一名优秀的程序员,十分优秀!