I have been using jQuery for a while. I wanted to use the parent()
selector. I also came up with the closest()
selector. Could not find any difference between them. Is there any? If yes, what?
我使用jQuery已经有一段时间了。我想使用父()选择器。我还提出了最近的()选择器。他们之间没有任何区别。有什么?如果是,什么?
What is the difference between parent(), parents() and closest()?
父()、父()和最近()之间的区别是什么?
5 个解决方案
#1
137
closest()
selects the first element that matches the selector, up from the DOM tree.close()从DOM树中选择第一个匹配选择器的元素。
parent()
selects one element up the DOM tree.父()选择DOM树中的一个元素。
parents()
method is similar toparent()
but selects all the matching elements up the DOM tree.parent()方法类似于parent(),但在DOM树中选择了所有匹配的元素。
#2
168
from http://api.jquery.com/closest/
从http://api.jquery.com/closest/
The .parents() and .closest() methods are similar in that they both traverse up the DOM tree. The differences between the two, though subtle, are significant:
.parents()和. nearest()方法很相似,它们都遍历DOM树。两者之间的差异虽然微妙,但却很明显:
.closest()
.closest()
- Begins with the current element
- 从当前元素开始
- Travels up the DOM tree until it finds a match for the supplied selector
- 遍历DOM树,直到找到所提供的选择器的匹配
- The returned jQuery object contains zero or one element
- 返回的jQuery对象包含0或1个元素。
.parents()
.parents()
- Begins with the parent element
- 从父元素开始
- Travels up the DOM tree to the document's root element, adding each ancestor element to a temporary collection; it then filters that collection based on a selector if one is supplied
- 沿着DOM树移动到文档的根元素,向临时集合添加每个祖先元素;然后,如果提供一个选择器,它将根据选择器筛选该集合
- The returned jQuery object contains zero, one, or multiple elements
- 返回的jQuery对象包含0、1或多个元素
.parent()
.parent()
- Given a jQuery object that represents a set of DOM elements, the .parent() method allows us to search through the parents of these elements in the DOM tree and construct a new jQuery object from the matching elements.
- 给定一个表示一组DOM元素的jQuery对象,.parent()方法允许我们在DOM树中搜索这些元素的父元素,并从匹配的元素中构造一个新的jQuery对象。
Note: The .parents() and .parent() methods are 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.
注意:.parents()和.parent()方法是相似的,除了后者只在DOM树上运行一个级别。另外,$(“html”).parent()方法返回一个包含文档的集合,而$(“html”).parents()返回一个空集合。
Here are related threads:
以下是相关的线程:
-
What's the difference between .closest() and .parents('selector')?
. nearest()和.parents('selector')之间有什么区别?
-
https://*.com/a/2200805/149206
https://*.com/a/2200805/149206
#3
11
The differences between the two, though subtle, are significant:
两者之间的差异虽然微妙,但却很明显:
.closest()
- Begins with the current element
- 从当前元素开始
- Travels up the DOM tree until it finds a match for the supplied selector
- 遍历DOM树,直到找到所提供的选择器的匹配
- The returned jQuery object contains zero or one element
- 返回的jQuery对象包含0或1个元素。
.parents()
- Begins with the parent element
- 从父元素开始
- Travels up the DOM tree to the document's root element, adding each ancestor element to a temporary collection; it then filters that collection based on a selector if one is supplied
- 沿着DOM树移动到文档的根元素,向临时集合添加每个祖先元素;然后,如果提供一个选择器,它将根据选择器筛选该集合
- The returned jQuery object contains zero, one, or multiple elements
- 返回的jQuery对象包含0、1或多个元素
From jQuery docs
从jQuery文档
#4
1
There is difference between both $(this).closest('div')
and $(this).parents('div').eq(0)
$(this). nearest ('div)和$(this).parents('div').eq(0)
Basically closest
start matching element from the current element whereas parents
start matching elements from parent (one level above the current element)
从当前元素开始最接近的匹配元素,而父元素从父元素开始匹配元素(当前元素之上的一个级别)
See http://jsfiddle.net/imrankabir/c1jhocre/1/
#5
0
$(this).closest('div')
is same as $(this).parents('div').eq(0)
.
(美元).closest(div)一样(这)美元.parents(div).eq(0)。
#1
137
closest()
selects the first element that matches the selector, up from the DOM tree.close()从DOM树中选择第一个匹配选择器的元素。
parent()
selects one element up the DOM tree.父()选择DOM树中的一个元素。
parents()
method is similar toparent()
but selects all the matching elements up the DOM tree.parent()方法类似于parent(),但在DOM树中选择了所有匹配的元素。
#2
168
from http://api.jquery.com/closest/
从http://api.jquery.com/closest/
The .parents() and .closest() methods are similar in that they both traverse up the DOM tree. The differences between the two, though subtle, are significant:
.parents()和. nearest()方法很相似,它们都遍历DOM树。两者之间的差异虽然微妙,但却很明显:
.closest()
.closest()
- Begins with the current element
- 从当前元素开始
- Travels up the DOM tree until it finds a match for the supplied selector
- 遍历DOM树,直到找到所提供的选择器的匹配
- The returned jQuery object contains zero or one element
- 返回的jQuery对象包含0或1个元素。
.parents()
.parents()
- Begins with the parent element
- 从父元素开始
- Travels up the DOM tree to the document's root element, adding each ancestor element to a temporary collection; it then filters that collection based on a selector if one is supplied
- 沿着DOM树移动到文档的根元素,向临时集合添加每个祖先元素;然后,如果提供一个选择器,它将根据选择器筛选该集合
- The returned jQuery object contains zero, one, or multiple elements
- 返回的jQuery对象包含0、1或多个元素
.parent()
.parent()
- Given a jQuery object that represents a set of DOM elements, the .parent() method allows us to search through the parents of these elements in the DOM tree and construct a new jQuery object from the matching elements.
- 给定一个表示一组DOM元素的jQuery对象,.parent()方法允许我们在DOM树中搜索这些元素的父元素,并从匹配的元素中构造一个新的jQuery对象。
Note: The .parents() and .parent() methods are 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.
注意:.parents()和.parent()方法是相似的,除了后者只在DOM树上运行一个级别。另外,$(“html”).parent()方法返回一个包含文档的集合,而$(“html”).parents()返回一个空集合。
Here are related threads:
以下是相关的线程:
-
What's the difference between .closest() and .parents('selector')?
. nearest()和.parents('selector')之间有什么区别?
-
https://*.com/a/2200805/149206
https://*.com/a/2200805/149206
#3
11
The differences between the two, though subtle, are significant:
两者之间的差异虽然微妙,但却很明显:
.closest()
- Begins with the current element
- 从当前元素开始
- Travels up the DOM tree until it finds a match for the supplied selector
- 遍历DOM树,直到找到所提供的选择器的匹配
- The returned jQuery object contains zero or one element
- 返回的jQuery对象包含0或1个元素。
.parents()
- Begins with the parent element
- 从父元素开始
- Travels up the DOM tree to the document's root element, adding each ancestor element to a temporary collection; it then filters that collection based on a selector if one is supplied
- 沿着DOM树移动到文档的根元素,向临时集合添加每个祖先元素;然后,如果提供一个选择器,它将根据选择器筛选该集合
- The returned jQuery object contains zero, one, or multiple elements
- 返回的jQuery对象包含0、1或多个元素
From jQuery docs
从jQuery文档
#4
1
There is difference between both $(this).closest('div')
and $(this).parents('div').eq(0)
$(this). nearest ('div)和$(this).parents('div').eq(0)
Basically closest
start matching element from the current element whereas parents
start matching elements from parent (one level above the current element)
从当前元素开始最接近的匹配元素,而父元素从父元素开始匹配元素(当前元素之上的一个级别)
See http://jsfiddle.net/imrankabir/c1jhocre/1/
#5
0
$(this).closest('div')
is same as $(this).parents('div').eq(0)
.
(美元).closest(div)一样(这)美元.parents(div).eq(0)。