Which is more efficient: .parent().parent().parent() ~or~ parents(“.foo”) ~or~ closest(“.foo”)

前端 未结 7 1793
-上瘾入骨i
-上瘾入骨i 2020-12-15 03:06

I have an A tag which triggers the animation of it\'s great-great-great-grandparent. All of the following will work, but which is most efficient, and why?

$         


        
7条回答
  •  抹茶落季
    2020-12-15 04:01

    I think I saw a presentation of John Resig saying that closest() is more optimized, and it makes some sense. Closest() is a newer addition to jQuery and comes to solve exactly this ugly parent().parent() chain. On the other hand parents() returns an array of parents that match your foo class and is more greedy in terms of searching compared with closest() that finds the first element and stops searching.

    I would bet that closest() is the most efficient if you are looking for the closest match.

提交回复
热议问题