html - 跳转到表格行并在 html 中突出显示它?
问题描述
我有一个长表,不同的条目相互关联,所以我将它们链接成这样:
<tr>
<th>Thing </th>
<th>related to Thing </th>
</tr>
<tr>
<td><a name = "t1"/>Thing 1</td>
<td><a href = "#t2">Thing2 is related </a></td>
</tr>
<tr>
<td><a name = "t2"/>Thing2</td>
<td><a href = "#t1">Thing1 is related </a></td>
</tr>
现在,我希望当我单击“Thing2 相关”时,我会跳下页面(哪个有效),但我希望该行很快亮起以指出哪一行是指。有什么办法吗?
解决方案
如果可以接受一点 jQuery,那么可以。您将不得不对您的 html 做一个小改动,将name
属性更改为id
.
// Listen for clicks on '.link' elements
$('table').on('click', '.link', function() {
// Find previously selected items and remove the class, restricting the search to the table.
$('.related', 'table').removeClass('related')
// Find the click target
target = $(this).attr('href');
// Find its 'tr' and highlight it
$(target).closest('tr').addClass('related')
});
.related {
background-color: #ccc;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>Thing </th>
<th>related to Thing </th>
</tr>
<tr>
<td><a id="t1" />Thing 1</td>
<td><a class="link" href="#t2">Thing2 is related </a></td>
</tr>
<tr>
<td><a id="t2" />Thing2</td>
<td><a class="link" href="#t1">Thing1 is related </a></td>
</tr>
</table>
推荐阅读
- excel - 在同一个 Excel 文件中导出三个不同大小的列表框
- r - 用 1 个 geom_point 和 2 个 geom_line 创建一个 ggplot
- r - 每次在R中将组ID分配给不同数量的行
- permissions - getUserMedia 区分什么硬件出错
- c - 我想知道如何传递整数指针的地址并通过取消引用来修改它,
- c++ - 如何使用 C++ 以编程方式更改 Word 文档中的变量?
- python - 仅当前缀和后缀匹配时,Python 才使用 re.sub 替换字符串
- python - 使用 Python 在 Selenium 中的 div 内滚动
- javascript - AJAX - 对预检请求的响应未通过访问控制检查
- ios - 使用 AFNetworking 进行 SSL 固定不起作用