javascript - 如果单击,则显示每个用户的表行

标签 javascript php html html-table

我正在努力解决以下问题。

我有一个包含用户的表格,当有人点击一行时,一个 div 应该显示在被点击的行的正下方并显示信息。首先,我正在构建表格:

<table id="table" class="table">
        <thead>
            //......
        </thead>
        <tbody>
        <?php $i = 1; foreach ($fetchStudenten as $student){ ?>
            <tr id="student">
                <td><?php echo $i ?></td>
                <td><?php echo $user['student_name'] ?></td>
                <td><?php echo $user['student_email'] ?></td>
                <td><?php echo $user['student_id'] ?></td>
            </tr>
            <?php if($i == 1){?><tr id="info" colspan="4" style="display: none" class="alert alert-info"><td colspan="4">Display here more info</td></tr><?php } ?>
            <?php $i++; } ?>
        </tbody>
    </table>

如您所见,我在表中为每个用户输入了值。我已经尝试了一些(纯!)JS 脚本,但我没有让它工作。是否有任何选项可以仅在 js 而不是 jQuery 中执行此操作?

最佳答案

"student"应该是一个类(id 是唯一的),你可以这样做:

var students=document.getElementsByClassName("students");
for(var id=0;id<students.length;id++){
  students[id].onclick=function(){
     this.parentNode.rows[ this.rowIndex + 1 ].style.display="block";
  };
}

关于javascript - 如果单击,则显示每个用户的表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42723078/

相关文章:

javascript - 什么是具有历史记录的跨浏览器 javascript 重定向的好解决方案?

javascript - jquery 组合链接 何时完成 何时完成 等等...何时

javascript - 在 jQuery 中使用 CSS 更改文本颜色

java - 使用 php 或 java 打开 Edge 数据库连接

php - 我从 MySQL 数据库中提取数据时出现奇怪的字符

php - 根据条件从数组中获取最小/最大值

javascript - 有没有一种方法可以以编程方式修复 javascript 中的 JSON 语法错误?

javascript - 点击 map 上的 angular2 openlayers 显示详细信息

php - 在模态中将 Jquery 变量传递给 php

html - 为什么我的 span 元素在 IE9 中看起来很糟糕?