jquery traversing to find a parent's parent

前端 未结 7 539
谎友^
谎友^ 2021-01-14 08:58

HTML:

  • 7条回答
    •  时光说笑
      2021-01-14 09:48

      .parent() by itself just refers to the immediate ancestor of a node, but you can also do:

      var li = $('a.del').parents('li.comment');
      

      to have JQuery go back up the DOM tree until it finds a matching ancestor node. In this case, li will point at the a's li ancestor.


      edit: forgot that .parent() only goes up one level, while .parents() goes up the entire tree. Both accept a selector argument, however.

    提交回复
  • 热议问题