jquery - 禁用单击每行中的最后一列

标签 jquery html twitter-bootstrap

我正在使用 Bootstrap 表。当前,当您单击列时,您会收到警报。我想要的是,当您单击最后一列时,什么也不会发生。感谢您的帮助。

$('#students tbody').on('click', 'tr.details-control', function() {
   alert('click');
 });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="students" class="table table-bordered table-striped table-hover">
<thead>
  <tr>
  <th>1</th>
  <th>2</th>
  <th>....</th>
  <th>Last</th>
  </tr>
</thead>
<tbody>
  <tr class="details-control">
    <td>First</td>
    <td>Second</td>
    <td>......</td>
    <td>No Alert</td>
  </tr>
  <tr class="details-control">
    <td>First</td>
    <td>Second</td>
    <td>......</td>
    <td>No Alert</td>
  </tr>
</tbody>
</table>

最佳答案

需要将 :not():last-child 选择器一起使用,如下所示:-

$('#students tbody').on('click', 'tr.details-control td:not(:last-child)', function() {
  alert('click');
});

工作示例:-

$('#students tbody').on('click', 'tr.details-control td:not(:last-child)', function() {
  alert('click');
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="students" class="table table-bordered table-striped table-hover">
<thead>
  <tr>
  <th>1</th>
  <th>2</th>
  <th>....</th>
  <th>Last</th>
  </tr>
</thead>
<tbody>
  <tr class="details-control">
    <td>First</td>
    <td>Second</td>
    <td>......</td>
    <td>No Alert</td>
  </tr>
  <tr class="details-control">
    <td>First</td>
    <td>Second</td>
    <td>......</td>
    <td>No Alert</td>
  </tr>
</tbody>
</table>

关于jquery - 禁用单击每行中的最后一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470559/

相关文章:

javascript - 单击按钮消失并显示隐藏字段

jQuery Mobile Slider - 刷新事件

javascript - grumble.js,jQuery 插件(气泡弹出窗口): how to make it not polute my document body with unremovable trash?

html - Bootstrap 表格内联样式不接触

javascript - 推特 Bootstrap3 : get current carousel index number via clicking and automatic

javascript - jQuery javascript 未正确返回 true/false

javascript - 页面完全加载后加载jquery

html - 快疯了...无法设置字体颜色

jquery - 在 Bootstrap 弹出窗口中渲染 React 组件

javascript - 在输入单选更改时禁用部分中的所有元素