jQuery 查找似乎不起作用 [英] jQuery find does not seem to work

查看:19
本文介绍了jQuery 查找似乎不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

When I try $(selectedContents.childNodes).find('span'), it returns an empty set!

However, when I print the "$(selectedContents.childNodes)[1].localName" it says "span"!

Is there anything wrong in my find? Please help.

Thanks
Srikanth

解决方案

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');

The .find() method is used to search for descendants.


EDIT: Note that your approach of passing the childNodes into the jQuery object is correct. You can't pass a documentFragment as some suggest.

Here's an example to illustrate: http://jsfiddle.net/P8nur/

这篇关于jQuery 查找似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆