Ignore table click function when clicking on a link

后端 未结 2 818
谎友^
谎友^ 2021-01-28 11:49

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

$(\'.table > tbody > tr\').click(function () {
    if ($(this).hasClass(\"info\")) {
        $(         


        
相关标签:
2条回答
  • 2021-01-28 12:30

    Just check if a link is clicked with event.target and .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");
        }
    });
    
    0 讨论(0)
  • 2021-01-28 12:40

    You can click event from propagating from the anchor

    $('.table > tbody > tr a').click(function (e) { 
        e.stopPropagation()
    });
    

    http://jsfiddle.net/JkebH/

    0 讨论(0)
提交回复
热议问题