Getting a parent element for a jquery selector

后端 未结 3 1733
轻奢々
轻奢々 2020-12-10 10:25

I have a table where each row has a CSS id like this:

........         
相关标签:
3条回答
  • 2020-12-10 10:27

    you can use this.

    $('.anotherclass').click(function () {
         alert($(this).parents('tr').attr("id"));
    });
    
    0 讨论(0)
  • 2020-12-10 10:30

    Use closest:

    $(".anotherclass").click(function() {
        console.log($(this).closest("tr").attr("id"));
    });
    

    From the docs:

    Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

    0 讨论(0)
  • 2020-12-10 10:39

    Use closest():

    $('.anotherclass').click(function () {
         alert($(this).closest('tr').prop('id'));
    });
    
    0 讨论(0)
提交回复
热议问题