jQuery: selecting grandparents

后端 未结 6 1072
悲哀的现实
悲哀的现实 2020-12-14 05:39

Is there a better way to select grandparent elements in jQuery in order to avoid this ?

$(this).parent().parent().parent().parent().parent().children(\".titl         


        
6条回答
  •  死守一世寂寞
    2020-12-14 06:25

    In addition to parents(), as they've said, you should also check out closest(). Read the comparison in the documentation there, but its main differences are that it searches for only one result, and it includes $(this) in what it searches (could get the thing you're searching from if you're not specific enough). Pros and cons.

提交回复
热议问题