[英]How to loop through an array calling jquery.each on each element?
I am trying to loop through an array of elements and then validate each instance of each element.我正在尝试遍历元素数组,然后验证每个元素的每个实例。 This is how I am doing it:这就是我的做法:
var elements = ["h1","h2","h3","h4","p","strong","label","span","a"];
function targetZWS(){
for (var i = 0; i < elements.length; i++) {
var item = $(".page-content "+elements[i]);
$(item).each(function() {
checkElement(this);
});
}
}
This throws a warning that I am creating a function inside a loop, how do I avoid this?这会发出一个警告,提示我正在循环中创建一个函数,我该如何避免这种情况?
You are trying too hard :) JQuery allows you to enter multiple options in a single selector.你太努力了 :) JQuery 允许你在一个选择器中输入多个选项。
function targetZWS(){
$("h1,h2,h3,h4,p,strong,label,span,a").each(function() {
checkElement(this);
});
}
}
http://api.jquery.com/multiple-selector/ http://api.jquery.com/multiple-selector/
Use .each()
to loop through the array as:使用.each()
循环遍历数组:
$(function() {
var elements = ["h1","h2","h3","h4","p","strong","label","span","a"];
$.each(elements, function(index, value) {
alert(value);
var sel = ".page-content" + value
var item = $("sel");
$(item).each(function() {
checkElement(this);
});
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.