jQuery查找似乎不起作用 [英] jQuery find does not seem to work
问题描述
我有一个DocumentFragment存储在"selectedContents"中,我正在尝试借助jQuery在其中找到"span"元素.它有两个子节点,第一个是文本节点,第二个是跨度.
I have a DocumentFragment stored in "selectedContents", and I am trying to find "span" elements in it, with the help of jQuery. It has two child nodes, where the first one is a text node, and the second one a span.
当我尝试$(selectedContents.childNodes).find('span')
时,它返回一个空集!
When I try $(selectedContents.childNodes).find('span')
, it returns an empty set!
但是,当我打印"$(selectedContents.childNodes)[1].localName"
时,它会显示"span"!
However, when I print the "$(selectedContents.childNodes)[1].localName"
it says "span"!
我的发现有什么问题吗?请帮忙.
Is there anything wrong in my find? Please help.
谢谢
斯里坎特
Thanks
Srikanth
推荐答案
由于要传递元素集合,因此需要使用.filter()
从集合中过滤出<span>
.
Because you're passing a collection of elements, you need to use .filter()
to filter the <span>
out of the set.
$(selectedContents.childNodes).filter('span');
- http://api.jquery.com/filter/
- http://api.jquery.com/filter/
.find()
方法用于搜索后代.
编辑:请注意,将childNodes
传递到jQuery对象中的方法是正确的.正如某些建议一样,您不能通过documentFragment
.
Note that your approach of passing the childNodes
into the jQuery object is correct. You can't pass a documentFragment
as some suggest.
这里有一个示例来说明: http://jsfiddle.net/P8nur/
这篇关于jQuery查找似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!