How To Select First Ancestor That Matches A Selector?

后端 未结 4 1296
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 08:11

General:

How can I select the first matching ancestor of an element in jQuery?

Example:

Take this HTML block

<
4条回答
  •  时光说笑
    2020-12-17 08:22

    You can pass a selector to the parent argument I believe.

    $('.remove').click(function(){
      $(this).parents("tr:first").hide();
      return false;
    });
    

    Or you can use closest as indicated by another answer.

    $('.remove').click(function(){
      $(this).closest("tr").hide();
      return false;
    });
    

提交回复
热议问题