数组的.get()函数不适用于jQuery

时间:2016-07-14 17:30:08

标签: jquery

var a = array.get(0);

这在jQuery脚本中不起作用,我收到错误array.get is not a function

1 个答案:

答案 0 :(得分:0)

.get()是jQuery对象上的方法,而不是数组对象上的方法。

// create jQuery object that contains references to all menus
var menus = $(".menu");

// get a regular array of DOM elements out of that jQuery object
var menuArray = menus.get();

// iterate that regular array
menuArray.forEach(function(item) {
    console.log(item);
});

这里是doc for jQuery's .get()

如果您已有数组,则只需使用数组语法访问数组元素:

var x = [1,2,3];

console.log(x[0]);   // 1