【问题标题】:JQuery - Clickable table row, except last cellJQuery - 可点击的表格行,除了最后一个单元格
【发布时间】:2011-09-06 17:35:22
【问题描述】:

我正在尝试使用 JQuery 使表格行可点击并重定向到隐藏在第一个单元格中的 URL。 我在表格的最后一列中有一张图片,应该重定向到不同的 URL。

JQuery如下。

$(function () {
    $('#link-table td:first-child').hide();

    $('#link-table tr').hover(function () {
        $(this).toggleClass('highlight');
    });

    $('#link-table tr').click(function () {
        location.href = $(this).find('td a').attr('href');
    });
});

单击该行有效,单击最后一个单元格中的图像超链接将重定向到与单击不是我想要的行相同的 URL。

我尝试将此代码用于点击事件

$('#link-table tr td:not(:last-child))').click(function () {
        location.href = $(this).find('td a').attr('href');
    });

单击最后一个单元格中的图像超链接有效,但单击该行现在重定向到附加到最后一个单元格中的图像超链接的 URL。

我怎样才能得到它,以便单击该行重定向到一个 URL,单击最后一个单元格中的超链接重定向到另一个?

【问题讨论】:

    标签: jquery


    【解决方案1】:
    $('#link-table tr td:not(:last-child)').click(function () {
            location.href = $(this).find('td a').attr('href');
        });
    

    您的$(this)'#link-table tr td:not(:last-child))',您可以通过以下方式找到tr var tr = $(this).closest('tr'); 然后你可以使用var tr 做任何你喜欢的事情

    然后找到'td a'

    【讨论】:

    • 这是正确的解决方案。那里有一个额外的支架,我认为这有点搞砸了。相反,它应该是 $('#link-table tr td:not(:last-child)').click
    【解决方案2】:

    您可能需要返回:

    $('#link-table tr').last().click(function () {
      return false;
    });
    

    【讨论】:

    • 您正在选择最后一行。他想要除行中最后一个 td 之外的所有行。
    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 2012-03-29
    • 2020-11-23
    • 1970-01-01
    相关资源
    最近更新 更多