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

有人知道我会怎么做吗?


当前回答

$('.testimonal').each(function(i,v){
  if (condition) {
    doSomething();
  }
});

其他回答

你可以使用.filter简单地做到这一点。下面的例子将隐藏所有包含单词"something"的. tribute div:

$(".testimonial").filter(function() {
    return $(this).text().toLowerCase().indexOf("something") !== -1;
}).hide();

你可以使用jQuery $each方法循环遍历所有的元素与类凭据。 I =>是集合中元素的索引,val给你特定元素的对象,你可以使用“val”来进一步访问元素的属性并检查你的条件。

$.each($('.testimonal'), function(i, val) { 
    if(your condition){
       //your action
    }
});

更精确的:

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

试试这个…

$('.testimonial').each(function(){
    //if statement here 
    // use $(this) to reference the current div in the loop
    //you can try something like...


    if(condition){

    }


 });

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>