Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.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
Javascript 使用jQuery隐藏表行onclick_Javascript_Jquery_Html Table_Jquery Selectors_Hide - Fatal编程技术网

Javascript 使用jQuery隐藏表行onclick

Javascript 使用jQuery隐藏表行onclick,javascript,jquery,html-table,jquery-selectors,hide,Javascript,Jquery,Html Table,Jquery Selectors,Hide,我有一大堆表格行,例如: <tr> <td>cell1</td> <td>cell2</td> <td><a href="action.php">cell3</a></td> </tr> <tr class="notes_row"> <td colspan="6"> <ul class="message warning no-margi

我有一大堆表格行,例如:

<tr>
  <td>cell1</td>
  <td>cell2</td>
  <td><a href="action.php">cell3</a></td>
</tr>
<tr class="notes_row">
<td colspan="6">
<ul class="message warning no-margin" id="notes_box">
<li>Notes here</li>
</ul>
</td>
</tr>
<tr>
  <td>cell1</td>
  <td>cell2</td>
  <td><a href="action.php">cell3</a></td>
</tr>

第1单元
细胞2
  • 注意这里
第1单元 细胞2
class=“notes\u行”仅当其上方的行存在注释时才存在。我如何隐藏tr,如果tr在那里,如何隐藏tr及其下面的notes_row类,而不影响使用jquery的其他行?因此,如果有人单击了cell3,链接所在的tr将被隐藏,如果下面有一个notes表行,它也将隐藏该链接

$('a[href=action.php]').click(function(){  // ... or however you attach to that link
    var row = $(this).closest('tr');

    // hide this row first
    row.hide();

    // next, get the next TR, see if it is a notes row, and hide it if it is
    var nxt = row.next();
    if (nxt.hasClass('notes_row'))
      nxt.hide();

    // stop link
    return false;
});
我想。。。凭记忆来的

编辑

两个小补丁和一个小提琴链接来查看它的运行:


这将是一个很好的地方,它可以简单到:

$("#tableID").delegate("a[href='action.php']", "click", function(e) {
  $(this).closest("tr").hide().next("tr.notes_row").hide();
  e.preventDefault(); //don't follow the action.php href
});

。这样做的目的是爬到
那一行,选择
(如果它有
.notes\u行
类)以及那一行。最后,我在那里添加了一个,这样我们就不会从链接本身导航到
action.php

您刚才问了一个类似的问题-请不要滥用这个社区。在要求他人为您编写代码之前,请阅读文档并了解jQuery的工作原理!去掉
.hasClass()
(它返回一个布尔值),改为执行
.next('.notes\u row').hide()
。如果下一行没有该类,则不会发生任何事情。@patrickdw:在我发布时捕捉到了它。现在都修好了(你能告诉我快睡觉了吗?)不过看起来不错,谢谢@帕特里克,这个答案应该是可以接受的。如果这一秒不可用,它很快就会出现(我相信接受给别人时间回答,而不是让人们总是第一个回答会有延迟(回答的速度很少是准确的同义词;p))。