javascript - 在表行悬停时调用函数
问题描述
我想调用一个函数,每次我将鼠标悬停在表格中时打印一行的内容。到目前为止,我有这个:
function tablemouseover(event) {
console.log(event.target);
}
<table>
<tr onmouseover='tablemouseover(event)'>
<td>times[row]</td>
<td>locations[row][0]</td>
<td>locations[row][1]</td>
<td>AllDistances[row]m</td>
</tr>
</table>
然而,这只是让我<td>
徘徊。
解决方案
用于closest('tr')
在 DOM 树中搜索最近的tr
父级,然后innerHTML
像这样记录它:
function tablemouseover(event){
console.log(event.target.closest('tr').innerHTML);
}
<table>
<tr onmouseover='tablemouseover(event)'>
<td>times[row]</td>
<td>locations[row][0]</td>
<td>locations[row][1]</td>
<td>AllDistances[row]m</td>
</tr>
</table>
推荐阅读
- c# - c# FlaUi 从另一个程序的 DataGridView 中获取所有值
- telegram - 如何使用 Telethon 将消息转发给其他联系人
- pine-script - 我有一个在 Pine Script 中找不到的函数脚本
- laravel - laravel 干预图像源在部署时不可读
- javascript - Javascript中最长的递减子序列子数组
- c++ - 将 unsigned int 的最大值转换为 int 并期望结果为-1 是否可移植?
- node.js - node-windows权限被拒绝-编译后不请求权限?
- c++ - 如果服务器每秒发送大约 180,000 字节的数据包,我是否需要考虑使用 IOCP 服务器和航位推算?
- python - 如何在字段上定义带有类型提示的 Python 类
- flutter - Image.network 或 NetworkImage 中的图像故障