我有一个div与类的证明,我想用jquery循环通过他们检查每个div如果一个特定的条件是真的负载。如果为真,它应该执行一个操作。
有人知道我会怎么做吗?
我有一个div与类的证明,我想用jquery循环通过他们检查每个div如果一个特定的条件是真的负载。如果为真,它应该执行一个操作。
有人知道我会怎么做吗?
当前回答
更精确的:
$.each($('.testimonal'), function(index, value) {
console.log(index + ':' + value);
});
其他回答
更精确的:
$.each($('.testimonal'), function(index, value) {
console.log(index + ':' + value);
});
divs = $('.testimonial')
for(ind in divs){
div = divs[ind];
//do whatever you want
}
你可以使用jQuery $each方法循环遍历所有的元素与类凭据。 I =>是集合中元素的索引,val给你特定元素的对象,你可以使用“val”来进一步访问元素的属性并检查你的条件。
$.each($('.testimonal'), function(i, val) {
if(your condition){
//your action
}
});
jQuery的.eq()可以帮助您使用索引方法遍历元素。
var testimonialElements = $(".testimonial");
for(var i=0; i<testimonialElements.length; i++){
var element = testimonialElements.eq(i);
//do something with element
}
你可以这样做
$('.testimonial').each(function(index, obj){
//you can use this to access the current item
});