如何用JavaScript隐藏父母的父元素?

时间:2016-07-27 12:30:17

标签: javascript html css

我有这个代码,我想按钮“接受”#39;单击,整个<tr>标记将被样式隐藏&#39; display:none;&#39;。此刻,按钮会隐藏<td>,我不希望它像那样。我希望它隐藏整个表格行。

<table class="table table-bordered">
  <tr>
    <th>Name</th>
    <th>Username</th>
    <th>Email</th>
    <th>Skype</th>
    <th>Phone</th>
    <th>Message</th>
    <th>Accept?</th>
  </tr>
  <tr style="">
    <td>Name</td>
    <td>Username</td>
    <td>email@gmail.com</td>
    <td>skypename</td>
    <td>phone</td>
    <td>MESSAGE</td>
    <td><button onclick="this.parentElement.style.display='none';" class="btn btn-success">Accept</button> <button class="btn btn-danger">Deny</button></td>
  </tr>
</table>

2 个答案:

答案 0 :(得分:1)

这样做:

<button onclick="this.parentElement.parentElement.style.display='none';" class="btn btn-success">Accept</button>

答案 1 :(得分:0)

既然你已经提到jQuery就可以这样做

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table class="table table-bordered">
      <tr>
        <th>Name</th>
        <th>Username</th>
        <th>Email</th>
        <th>Skype</th>
        <th>Phone</th>
        <th>Message</th>
        <th>Accept?</th>
      </tr>
      <tr style="">
        <td>Name</td>
        <td>Username</td>
        <td>email@gmail.com</td>
        <td>skypename</td>
        <td>phone</td>
        <td>MESSAGE</td>
        <td><button onclick="$(this).parents('tr').hide()" class="btn btn-success">Accept</button> <button class="btn btn-danger">Deny</button></td>
      </tr>
    </table>