单击链接时忽略表单击功能 [英] Ignore table click function when clicking on a link

查看:109
本文介绍了单击链接时忽略表单击功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在我的表格中点击一行时,我有一个点击方法。

I have a click method when the user taps a row in my table.

$('.table > tbody > tr').click(function () {
    if ($(this).hasClass("info")) {
        $(this).removeClass("info");
    }
    else {
        $(this).addClass("info");
    }
});

但是,某些单元格中有链接,如果点击链接我想忽略上面的方法。我该怎么做?

However, there are links in some of the cells, if a link is clicked i would like to ignore the method above. How can i do this?

推荐答案

只需检查是否用 event.target点击链接 .closest()

$('.table > tbody > tr').click(function (e) { //Catch event here
    if($(e.target).closest('a').length) return; // Add this
    if ($(this).hasClass("info")) {
        $(this).removeClass("info");
    }
    else {
        $(this).addClass("info");
    }
});

这篇关于单击链接时忽略表单击功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆