我有一个div与类的证明,我想用jquery循环通过他们检查每个div如果一个特定的条件是真的负载。如果为真,它应该执行一个操作。
有人知道我会怎么做吗?
我有一个div与类的证明,我想用jquery循环通过他们检查每个div如果一个特定的条件是真的负载。如果为真,它应该执行一个操作。
有人知道我会怎么做吗?
当前回答
divs = $('.testimonial')
for(ind in divs){
div = divs[ind];
//do whatever you want
}
其他回答
没有jQuery更新
document.querySelectorAll(“.testimonial”)。forEach(函数(元素,索引){ 元素。innerHTML = '感言' +(索引+ 1); }); < div class = "证明" > < / div > < div class = "证明" > < / div >
jQuery的.eq()可以帮助您使用索引方法遍历元素。
var testimonialElements = $(".testimonial");
for(var i=0; i<testimonialElements.length; i++){
var element = testimonialElements.eq(i);
//do something with element
}
你可以使用.filter简单地做到这一点。下面的例子将隐藏所有包含单词"something"的. tribute div:
$(".testimonial").filter(function() {
return $(this).text().toLowerCase().indexOf("something") !== -1;
}).hide();
使用简单的for循环:
var testimonials= $('.testimonial');
for (var i = 0; i < testimonials.length; i++) {
// Using $() to re-wrap the element.
$(testimonials[i]).text('a');
}
试试这个…
$('.testimonial').each(function(){
//if statement here
// use $(this) to reference the current div in the loop
//you can try something like...
if(condition){
}
});