数组有点像这样:
[[Object { button={...}},
Object { input={...}},
Object { checkbox={...}},
Object { textarea={...}}],
[Object { textarea={...}}]
]
在大括号中我设置了一些属性,如颜色、值、类型等。
我想要的是获取数组的每个对象并检查对象类型等属性,然后调用函数来执行进一步的操作。就像在 PHP 中我们使用:
foreach($a as $b){
// and then do something here ..
};
请帮助我,我希望每个人都能理解我想说的。
// var i is the counter for page numbers
function pagination(i) {
alert(i);
i--;
//page is array
var result = page;
//console.log(result[i]);
var $currentElem;
$(result[i]).each(function() {
currentElem = $(this);
console.log(currentElem);
});
}
最佳答案
.each
在循环 jQuery 集合的元素时使用。循环遍历数组或对象的内容。使用$.each()
:
$.each(result[i], function(n, currentElem) {
console.log(currentElem);
});
并且您不应该使用 $(this)
除非 this
是 DOM 元素。如果它只是一个 Javascript 对象,则不需要将其包装在 jQuery 对象中。
您可以使用普通的 Javascript variable.propertyname
语法访问属性,例如currentElem.button
和 currentElem.button.color
。要将元素附加到 View 中,您可以执行以下操作:
var button = currentElem.button;
$("<button>", {
value: button.value,
name: button.name,
css: {
color: button.color,
width: button.width,
backgroundColor: button.backgroundcolor
}
}).appendTo($("#buttonDiv");
关于jquery - jquery中如何访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31782662/