开发者

How to convert the result of jQuery .find() function to an array?

What does jQuery .find() method return? a object OR a array list of objects?

If it returns an object which contain all the matched elements. How to convert this object to an array?

If it returns a array of elements, why $(xml).find("DATE").sort(mySortFunc); does not wor开发者_如何学Ck, it seems the jQuery .find() returns an object which can not apply Javascript sort() method which is supposed to be applied on array.

Generally, I need to sort the objects find by $(xml).find("DATE") , but when I use sort function, it raised an error that the object can not be resolved.


The majority of jQuery methods returns a jQuery object, which can be accessed like it is an array (e.g. it has a .length attribute, elements can be accessed using the square bracket notation ([0]), and it supports some array methods (slice())).

jQuery has a method called toArray() which can be used to convert the jQuery object to a real array.

You can also use get() with no arguments to achieve the same effect (and save you a few key presses).

In future, you can checkout the jQuery API, and the return type for all jQuery methods is listed in the relevant documentation (e.g. for find(), the return type is "jQuery")


If you call .get() on a jQuery object without a parameter, it will return a regular array of DOM elements.


jQuery already acts like an array, and thus you can apply array like functionality to it.

Try to change

$(xml).find("DATE").sort(mySortFunc);

with

Array.prototype.sort.apply($(xml).find("DATE"), mySortFunc);

and you should get what you need

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜