JQuery: Get the parent element id of a clicked element

后端 未结 4 416
鱼传尺愫
鱼传尺愫 2021-01-18 01:38

I have a div like this:

x
4条回答
  •  攒了一身酷
    2021-01-18 02:05

    You should do

    $(".popupCloseClass").click(function (event) {
        var id = $(this).closest("div").attr("id");
    });
    

    (you could also use $(this).parent().attr("id"); ) but using closest is safer in case you change your html structure

提交回复
热议问题