javascript - 如何使用按钮禁用表格行的折叠?

标签 javascript jquery html twitter-bootstrap

我已经安装了单击表行时的 Bootstrap 折叠功能。现在,如果您注意到,可以通过单击行上的任意位置来打开折叠,这很好,这就是我想要的方式。

但是,如果您注意到表格行的最后一列有一个桌面屏幕,我希望有一个异常(exception)。如果我点击它,我不希望该行打开 Bootstrap 折叠。

有什么办法可以实现这个目标吗?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="//stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin=" anonymous"></script>


<table class="table">
  <thead class="table-header">
    <tr>
      <th scope="col"></th>
      <th scope="col">ID</th>
      <th scope="col">Name</th>
      <th scope="col">Type</th>
      <th scope="col">Status <span class="badge badge-danger profile-verification-noti">4</span></th>
      <th scope="col">Last Login</th>
      <th scope="col"></th>
    </tr>
  </thead>
  <tbody>
    <tr class="table-chevron" data-toggle="collapse" data-target="#AccountDetails">
      <td><a href="javascript:void(1);" class="right-angle option-tab"><i class="fas fa-angle-right"></i></a></td>
      <td>[0708]</td>
      <td>Mark Jonas</td>
      <td>Guest</td>
      <td class="success">Active</td>
      <td>22/11/2018</td>
      <td><a href="http://google.com" target="_blank" class="fa-user-imp-text"><i class="fas fa-desktop"></i></a></td>
    </tr>

    <!-- Table Row 1 Collapse -->
    <tr>
      <td class="insert-here coll-bg" colspan="8">
        <!-- START OF COMPLETE ACCOUNT SETTINGS -->
        <div class="collapse" id="AccountDetails">
          <p>Hello World</p>
        </div>

最佳答案

您可以尝试Event​.stop​Propagation()在最后一个td上,防止当前事件在捕获和冒泡阶段进一步传播

$('tr td:last-child').click(function (e) {
  e.stopPropagation();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="//stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin=" anonymous"></script>


<table class="table">
  <thead class="table-header">
    <tr>
      <th scope="col"></th>
      <th scope="col">ID</th>
      <th scope="col">Name</th>
      <th scope="col">Type</th>
      <th scope="col">Status <span class="badge badge-danger profile-verification-noti">4</span></th>
      <th scope="col">Last Login</th>
      <th scope="col"></th>
    </tr>
  </thead>
  <tbody>
    <tr class="table-chevron" data-toggle="collapse" data-target="#AccountDetails">
      <td><a href="javascript:void(1);" class="right-angle option-tab"><i class="fas fa-angle-right"></i></a></td>
      <td>[0708]</td>
      <td>Mark Jonas</td>
      <td>Guest</td>
      <td class="success">Active</td>
      <td>22/11/2018</td>
      <td><a href="http://google.com" target="_blank" class="fa-user-imp-text"><i class="fas fa-desktop"></i></a></td>
    </tr>

    <!-- Table Row 1 Collapse -->
    <tr>
      <td class="insert-here coll-bg" colspan="8">
        <!-- START OF COMPLETE ACCOUNT SETTINGS -->
        <div class="collapse" id="AccountDetails">
          <p>Hello World</p>
        </div>

关于javascript - 如何使用按钮禁用表格行的折叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55998701/

相关文章:

javascript - 如何使用 jquery 在表行中动态创建的选择框中填充数据

javascript - 在不同的对象上应用相同的 id 并分别为它们设置动画 (jQuery)

javascript - 使用 jQuery 创建动画列表

javascript - 通过 redux-form、reducer 和 action Creator 发送数据表单

javascript - 将数据附加到 .each 循环中的 div 元素中

javascript - 当返回嵌套在 then block 中的 Promise 时,使用 Promise.all 的 map 会返回未定义的数组

php - 如何创建自定义 jquery 轮播?

JavaScript 检查总和是否正确

javascript - ng-controller 输出不正确

无需按 ctrl+f5 即可在线重新加载 html