javascript - PHP Js 只能使用表格的第一行(按钮)
问题描述
我是新手,仍然对我的问题感到困惑,所以这是我的 php 表
<table>
<tr>
<th align="center">ID</th>
<th align="center">Project Name</th>
<th align="center">Due Date</th>
<th align="center">Sub Date</th>
<th align="center">Comment</th>
<th align="center">Status</th>
<th align="center">Option</th>
</tr>
<tr>
<?php
while ($res2=mysqli_fetch_assoc($result2)) {
echo "<tr>";
echo "<td>".$res2['project_id']."</td>";
echo "<td>".$res2['project_name']."</td>";
echo "<td>".$res2['duedate']."</td>";
echo "<td>".$res2['subdate']."</td>";
echo "<td>\"".$res2['comment']."\"</td>";
echo "<td>".$res2['status']."</td>";
//as you can see, id = myId
echo "<td><a href=\"#\" id=\"myId\" class=\"button\">View</a>";
}?>
</td>
</tr>
</table>
据说当点击每一行旁边的按钮时,会出现一个弹出窗口,但只有第一行有效,其他按钮什么也不做。我确实已经搜索了 2 个小时左右的这个问题,他们中的大多数都在谈论唯一 id,但是我该如何实现或解决这个问题。
这是一个脚本
document.getElementById('myId').addEventListener("click", function () {
document.querySelector('.bg-modal').style.display = "flex";
});
非常感谢您的帮助,非常感谢。
解决方案
首先,您应该为每个 HTML 元素使用唯一 ID,如果您打算创建具有相同行为的多个元素,请class
改用。
将其更改为 后class
,您可以通过使用 分配一次来选择所有元素querySelectorAll()
。
这是工作示例:
document.querySelectorAll('button.click').forEach(elem =>
{
elem.addEventListener('click', () => {
document.querySelector('div').style.background = elem.innerText;
});
});
div {
width: 200px;
height: 200px;
}
<button id="1" class="click">green</button>
<button id="2" class="click">blue</button>
<div></div>
推荐阅读
- c++ - d3dx12.h 给出了一堆错误
- string - 堆栈上 char 缓冲区/ASCII 字符串的 Rust 等价物是什么?
- amazon-web-services - AWS CLI DynamoDB 向现有表添加非键属性
- html - Live Sass 编译器在最新的 VSCode 后导致扩展主机崩溃
- c# - 如何将 c++ 自定义类导入 c# unity
- python - 使用正则表达式在文本中查找文字字符串
- python - 将对象方法传递给多处理进程
- kotlin - Kotlin 字符串连接 - 使用多行字符串保留缩进
- php - Mysqli_fetch_array 不会显示结果。我不知道怎么了
- reactjs - ReactJs:没有索引的子键道具