【发布时间】:2021-12-28 18:13:29
【问题描述】:
我想在点击锚点时通过背景颜色突出显示目标行
<table>
<tr> <!-- highlight me! -->
<td>
<span id="A-1PP-05">A-1PP-05</span>
</td>
</tr>
<tr>...</tr>
<tr>...</tr>
</table>
<script>
(function($) {
$(document).on('click', 'a[href^="#"]', function(e) {
// target element id
var id = $(this).attr('href');
// target element
var $id = $(id);
// top position relative to the document
var pos = $id.offset().top;
// animated top scrolling
$('body, html').animate({scrollTop: pos - 120});
});
$(':target').closest('tr').css('background', 'yellow');
})(jQuery);
</script>
这行代码不起作用:
$(':target').closest('tr').css('background', 'yellow');
单击锚点只会向下滚动到带有id 的<span>,但不会突出显示其父元素<tr>。
【问题讨论】:
-
您的点击处理程序以
a链接为目标,但您的 HTML 不包含a链接。 -
您的
$(':target')行不应该在您的点击处理程序中吗?
标签: jquery