选择除前两列中的单元格外的所有单元格

Select all cells except ones from first two columns

本文关键字:单元格 两列 选择      更新时间:2023-09-26

使用数据表,我使所有<td>都可以单击。我如何告诉函数从前两行中排除单元格,因为我不希望这些单元格是可单击的?

function () {
        var api = this.api();
        api.$('td').click( function () {
            api.search( this.innerHTML ).draw();
        });},

我的桌子:

<table>
    <thead>
            <tr>                
                <th>Name</th>
                <th>Surname</th>
                <th>etc</th>
                <th>etc</th>
            </tr>
    </thead>
    <tbody>
            <?php foreach ($records as $record) : ?>
            <tr>
                <td><?php e($record->name); ?></td>
                <td><?php e($record->surname) ?></td>
                <td><?php e($record->etc) ?></td>
                <td><?php e($record->cetc) ?></td>
            </tr>
            <?php endforeach; ?>
    </tbody>
</table>

您可以使用jquery gt()排除前两行,请参阅下面的代码

function () {
        var api = this.api();
        api.$('tr:gt(1) td').click( function () {
            api.search( this.innerHTML ).draw();
        });},

gt()API DOC

您可以为此使用not()方法:

api.$('td').not(":nth-child(1), :nth-child(2)").click( function () {

您可以为此使用lt选择器。

$('tr:lt(2) td').click(function() {
    api.search(this.innerHTML).draw();
});

这将把事件绑定到前2行中的td

编辑

如果您想将事件绑定到除前2行之外的行,请使用以下

$('tr:gt(1) td').click(function() {
        api.search(this.innerHTML).draw();
  });