Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JQuery在单击其内容后获取tr<;a>;_Jquery - Fatal编程技术网

JQuery在单击其内容后获取tr<;a>;

JQuery在单击其内容后获取tr<;a>;,jquery,Jquery,如果我有 <table> <tr> <td><a class='ilink'> link text </a></td> <td></td> <td></td> <tr> <tr> <td><a class='ilink'> link text </a></td>

如果我有

<table>
  <tr>
    <td><a class='ilink'> link text </a></td>
    <td></td>
    <td></td>
  <tr>
  <tr>
    <td><a class='ilink'> link text </a></td>
    <td></td>
    <td></td>
  </tr>
</table>

链接文本
链接文本
在jquery代码中,单击链接后,我想突出显示链接所在的整个表行。但是我怎样才能找到它呢?

你可以这样做:

$("a.ilink").click(function() {
  $(this).closest("tr").addClass("highlight");
});
如果您有很多行,这将更有效(一份副本,而不是每
一份副本):

然后需要定义高亮显示css类:

.hightlight { background-color:red; }

回答得好。我本来打算推荐
parentsUntil()
,但不知道它的集合在DOM树上运行时包含所有父项。“最近的”很有道理+1.
#EDIT remove...  better options listed
$(document).ready(function(){
    $('a.ilink').click(function() {
        $('tr').removeClass('highlight');
        $(this).closest('tr').addClass('highlight');
    });
 });
.hightlight { background-color:red; }