How to know element clicked within a table?

前端 未结 2 1638
执笔经年
执笔经年 2021-01-21 05:26

I\'m trying get the element clicked within a TR in a HTML table. If I click on the Select input inside a TR, CurrentTarget field returns \"TR\", and OriginalT

2条回答
  •  暖寄归人
    2021-01-21 06:15

    You haven't actually said what trb is but it sounds like it might be a set of the tr elements in your table.

    What you're looking for is elem.target. That's the topmost element that was clicked, the one that initiated the event. (FWIW, I wouldn't call the argument passed to the event handler elem, it's an event, not an element.)

    For instance, if you have:

    Click me

    ...and this:

    $("tr").click(function(e) {
        console.log(e.target.tagName);
    });
    

    ...and you click the text "click me," you'll see

    STRONG

    ...in the console.


    Side note: It's handy to use closest with that if you want to know what cell or row was clicked, e.g.:

    var $td = $(e.target).closest('td');
    

提交回复
热议问题