jQuery循环遍历具有相同类的元素

我有一个带有类testimonial的div负载,我想使用jQuery循环遍历它们以检查每个div是否有特定条件为真。如果是真的,它应该执行一个操作。

有谁知道我该怎么做吗?

1024633 次浏览

试试这个…

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




if(condition){


}




});

使用each:'i'是数组中的位置,obj是您正在迭代的DOM对象(也可以通过jQuery包装器$(this)访问)。

$('.testimonial').each(function(i, obj) {
//test
});

查看api引用以获取更多信息。

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

你可以这样做

$('.testimonial').each(function(index, obj){
//you can use this to access the current item
});
divs  = $('.testimonial')
for(ind in divs){
div = divs[ind];
//do whatever you want
}

我可能错过了问题的一部分,但我相信你可以简单地做到这一点:

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

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

您可以使用.filter简明扼要地执行此操作。以下示例将隐藏所有包含单词“某物”的.见证div:

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

jQuery的. eq()可以帮助您使用索引方法遍历元素。

var testimonialElements = $(".testimonial");
for(var i=0; i<testimonialElements.length; i++){
var element = testimonialElements.eq(i);
//do something with element
}

如今,在没有jQuery的情况下做到这一点非常简单。

没有jQuery:

只需选择元素并使用.forEach()方法迭代它们:

const elements = document.querySelectorAll('.testimonial');
Array.from(elements).forEach((element, index) => {
// conditional logic here.. access element
});

在旧浏览器中:

var testimonials = document.querySelectorAll('.testimonial');
Array.prototype.forEach.call(testimonials, function(element, index) {
// conditional logic here.. access element
});

试试这个例子

Html

<div class="testimonial" data-index="1">
Testimonial 1
</div>
<div class="testimonial" data-index="2">
Testimonial 2
</div>
<div class="testimonial" data-index="3">
Testimonial 3
</div>
<div class="testimonial" data-index="4">
Testimonial 4
</div>
<div class="testimonial" data-index="5">
Testimonial 5
</div>

当我们想要访问那些data-index大于2divs时,我们需要这个jQuery。

$('div[class="testimonial"]').each(function(index,item){
if(parseInt($(item).data('index'))>2){
$(item).html('Testimonial '+(index+1)+' by each loop');
}
});

工作示例小提琴

更精确:

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

使用简单的for循环:

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

没有jQuery更新

document.querySelectorAll('.testimonial').forEach(function (element, index) {
element.innerHTML = 'Testimonial ' + (index + 1);
});
<div class="testimonial"></div>
<div class="testimonial"></div>

在JavaScriptES6. for每个() 通过Element.querySelectorAll()给出的类数组NodeList集合

document.querySelectorAll(".testimonial").forEach((el, idx) => {
el.style.color = "red";
console.log(`${idx} Element ${el.tagName} with ID #${el.id} says: ${el.textContent}` );
});
<p class="testimonial" id="1">This is some text</p>
<div class="testimonial" id="2">Lorem ipsum</div>

您可以使用jQuery$each方法循环遍历具有类证明的所有元素。 i=>是集合中元素的索引,val为您提供该特定元素的对象,您可以使用“val”进一步访问元素的属性并检查您的条件。

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