我有一个div与类的证明,我想用jquery循环通过他们检查每个div如果一个特定的条件是真的负载。如果为真,它应该执行一个操作。

有人知道我会怎么做吗?


当前回答

没有jQuery更新

document.querySelectorAll(“.testimonial”)。forEach(函数(元素,索引){ 元素。innerHTML = '感言' +(索引+ 1); }); < div class = "证明" > < / div > < div class = "证明" > < / div >

其他回答

使用简单的for循环:

var testimonials= $('.testimonial');
for (var i = 0; i < testimonials.length; i++) {
  // Using $() to re-wrap the element.
  $(testimonials[i]).text('a');
}

更精确的:

$.each($('.testimonal'), function(index, value) { 
    console.log(index + ':' + value); 
});

你可以这样做

$('.testimonial').each(function(index, obj){
    //you can use this to access the current item
});

JavaScript ES6 .forEach() 通过Element.querySelectorAll()给出的类似数组的NodeList集合

document.querySelectorAll(“.testimonial”)。forEach((el, idx) => { El.style.color = "红色"; console.log(' ${idx}元素${el. log。tagName}, ID为#${el。Id}表示:${el。textContent}’); }); <p class="证言" id="1">这是一些文本</p> <div class="证物" id="2">Lorem ipsum</div>

我可能忽略了部分问题,但我相信你可以简单地这样做:

$('.testimonial').each((index, element) => {
    if (/* Condition */) {
        // Do Something
    }
});

这使用jQuery的每个方法:https://learn.jquery.com/using-jquery-core/iterating/