jQuery find which parent is closer?

前端 未结 5 817
温柔的废话
温柔的废话 2021-01-13 17:52

In jQuery you can call closest to find the closest parent.

If I have a a in a li in a ul in a td in

5条回答
  •  佛祖请我去吃肉
    2021-01-13 18:25

    You can list multiple element selectors to the closest function, it will only find the closest of one of them:

    $('a').closest('table, ul')
    

    Example:

    $(document).on("click", function( event ) {
      $(event.target).closest("li, b").toggleClass("hilight");
    });
    .hilight{
      background: rgba(255, 0, 0, 0.5);
    }
    
    
    • Click me!
    • You can also click this line or click me!

提交回复
热议问题