sourcecode

jQuery parent(), parent() 및 가장 가까운() 함수 간의 차이

codebag 2023. 11. 4. 10:38
반응형

jQuery parent(), parent() 및 가장 가까운() 함수 간의 차이

저는 한동안 jQuery를 사용했습니다.나는 그것을 사용하고 싶었습니다.parent()선택자저도 생각해냈습니다.closest()선택자둘 사이의 차이를 찾을 수 없습니다.있나요?만약 그렇다면, 뭐?

사이의 차이점은 무엇입니까?parent(),parents()그리고.closest()?

http://api.jquery.com/closest/ 에서

.parent().close() 메서드는 둘 다 DOM 트리를 이동한다는 점에서 유사합니다.이 둘 사이의 차이점은 미묘하기는 하지만 중요합니다.

.closest()

  • 현재 요소로 시작합니다.
  • 제공된 선택기와 일치하는 선택기를 찾을 때까지 DOM 트리 위로 이동합니다.
  • 반환된 jQuery 개체에 0개 또는 하나의 요소가 포함되어 있습니다.

부모님들

  • 상위 요소로 시작합니다.
  • DOM 트리를 문서의 루트 요소로 이동하여 각 상위 요소를 임시 컬렉션에 추가한 다음, 선택기를 기준으로 해당 컬렉션을 필터링합니다.
  • 반환된 jQuery 개체에 0개, 1개 또는 여러 개의 요소가 포함되어 있습니다.

.부모님

  • DOM 요소 집합을 나타내는 jQuery 객체가 주어지면 .parent() 메서드를 사용하면 DOM 트리에서 이러한 요소의 상위를 검색하고 일치하는 요소로부터 새로운 jQuery 객체를 구성할 수 있습니다.

참고: .parent() 및 .parent() 메서드는 DOM 트리 위의 한 단계만 이동한다는 점을 제외하고는 비슷합니다.또한 $("html").parent() 메서드는 문서를 포함하는 집합을 반환하고 $("html").parent() 메서드는 빈 집합을 반환합니다.

관련 스레드는 다음과 같습니다.

closest() 선택기와 일치하는 첫 번째 요소를 DOM 트리에서 위로 선택합니다.현재 요소에서 시작하여 위로 이동합니다.

parent() DOM 트리에서 한 개의 요소(단일 레벨 업)를 선택합니다.

parents() 메서드는 DOM 트리 위의 모든 일치 요소와 유사하지만 선택합니다.상위 요소에서 시작하여 위로 이동합니다.

이 둘 사이의 차이점은 미묘하기는 하지만 중요합니다.

.closest()

  • 현재 요소로 시작합니다.
  • 제공된 선택기와 일치하는 선택기를 찾을 때까지 DOM 트리 위로 이동합니다.
  • 반환된 jQuery 개체에 0개 또는 하나의 요소가 포함되어 있습니다.

부모님들

  • 상위 요소로 시작합니다.
  • DOM 트리를 문서의 루트 요소로 이동하여 각 상위 요소를 임시 컬렉션에 추가한 다음, 선택기를 기준으로 해당 컬렉션을 필터링합니다.
  • 반환된 jQuery 개체에 0개, 1개 또는 여러 개의 요소가 포함되어 있습니다.

jQuery 문서에서 온 문서

둘 다 차이가 있습니다.$(this).closest('div')그리고.$(this).parents('div').eq(0)

기본적으로closest현재 요소에서 일치 요소를 시작합니다.parents상위 요소에서 일치 요소 시작(현재 요소보다 한 단계 위)

http://jsfiddle.net/imrankabir/c1jhocre/1/ 참조

$(this).closest('div')와 같은$(this).parents('div').eq(0).

parent()method는 선택한 요소의 직접 부모 요소를 반환합니다.이 메서드는 DOM 트리 위의 단일 레벨에서만 이동합니다.

parents()메서드를 사용하면 DOM 트리에서 이러한 요소의 조상을 검색할 수 있습니다.주어진 셀렉터에서 시작하여 위로 이동합니다.

The **.parents()** and **.parent()** methods are almost similar, except that the latter only travels a single level up the DOM tree. Also, **$( "html" ).parent()** method returns a set containing document whereas **$( "html" ).parents()** returns an empty set.

[closest()][3]method returns the first ancestor of the selected element.An ancestor is a parent, grandparent, great-grandparent, and so on.

This method traverse upwards from the current element, all the way up to the document's root element (<html>), to find the first ancestor of DOM elements.

According to docs:

**closest()** method is similar to **parents()**, in that they both traverse up the DOM tree. The differences are as follows:

**closest()**

Begins with the current element
Travels up the DOM tree and returns the first (single) ancestor that matches the passed expression
The returned jQuery object contains zero or one element

**parents()**

Begins with the parent element
Travels up the DOM tree and returns all ancestors that matches the passed expression
The returned jQuery object contains zero or more than one element





 

언급URL : https://stackoverflow.com/questions/9193212/difference-between-jquery-parent-parents-and-closest-functions

반응형