jQuery click event on parent, but finding the child (clicked) element

前端 未结 3 1875
萌比男神i
萌比男神i 2020-12-05 04:09

let say I have a parent element which has so many nested child elements inside of itself:

3条回答
  •  无人及你
    2020-12-05 05:11

    If your elements have grandchildren, there is a better way to get just the child. Note the greater than sign.

    $('.parent > .child').click(function() {
        // This will be the child even if grandchild is clicked
        console.log($(this));
    });
    

提交回复
热议问题