我试图遍历一个元素数组,然后验证每个元素的每个实例。我就是这样做的:
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);
});
}
}
这会抛出一个警告,我在循环中创建一个函数,我该如何避免这个?
答案 0 :(得分:1)
你太努力了:) JQuery允许你在一个选择器中输入多个选项。
function targetZWS(){
$("h1,h2,h3,h4,p,strong,label,span,a").each(function() {
checkElement(this);
});
}
}
答案 1 :(得分:0)
使用.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);
});
})
})
<强> DEMO 强>