我有一行代码,它使用mootools获取带有给定选择器的元素数组
var menuItems = $$('#container .menu');
我需要将其转换为jquery,但找不到解决方案。我尝试过容器(‘#jQuery .menu'),但它不返回数组。
有没有办法在整个文档上使用jquery中的'.find()‘?(因为我没有父元素来使它像parent.find()..)
我们也非常欢迎任何其他建议。
发布于 2012-02-01 19:05:49
使用jQuery实现您的语句:
jQuery('#container .menu')
将返回一个包含所有匹配元素的jQuery对象,您可以在其中使用类似数组的语法访问各个DOM元素:
var menuItems = jQuery('#container .menu');
menuItems.length // gives a count of how many matched
menuItems[0] // the first matching element
// but you can also use jQuery methods on the object
menuItems.hide();
如果您想要一个实际的数组而不是一个类似数组的对象,可以使用jQuery的.toArray()
method
var menutItemsArray = jQuery('#container .menu').toArray();
你为什么不试试jQuery website上的one of the jQuery tutorials呢
发布于 2012-02-01 19:08:35
如果您绝对需要它作为一个数组,而不是使用选择器返回的jQuery对象,那么可以看看.toArray()
函数。
https://stackoverflow.com/questions/9094690
复制相似问题