jQuery循环使用相同类的元素

时间:2011-01-19 12:41:30

标签: javascript jquery jquery-selectors

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

有谁知道我会怎么做?

15 个答案:

答案 0 :(得分:906)

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

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

查看api reference以获取更多信息。

答案 1 :(得分:108)

试试这个......

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


    if(condition){

    }


 });

答案 2 :(得分:42)

这些天没有jQuery这样做非常简单。

没有jQuery:

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

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

答案 3 :(得分:39)

试试这个例子

<强> 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>

当我们要访问divs大于data-index的{​​{1}}时,我们需要这个jquery。

2

Working example fiddle

答案 4 :(得分:27)

你可以这样做

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

答案 5 :(得分:17)

divs  = $('.testimonial')
for(ind in divs){
  div = divs[ind];
  //do whatever you want
}

答案 6 :(得分:14)

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

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

答案 7 :(得分:13)

您可以使用.filter简洁地完成此操作。以下示例将隐藏包含单词“something”的所有.testimonial div:

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

答案 8 :(得分:9)

使用简单的for循环:

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

答案 9 :(得分:8)

我可能会遗漏部分问题,但我相信你可以这样做:

$('.testimonial').each(function() {
    if(...Condition...) {
        ...Do Stuff...
    }
}

答案 10 :(得分:7)

没有更新jQuery

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

答案 11 :(得分:6)

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

答案 12 :(得分:4)

更精确:

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

答案 13 :(得分:2)

使用spread ... operator

在JavaScript ES6
YYYY-MM-DD, INT, INT, INT
YYYY-MM-DD, INT, INT, INT

[...document.querySelectorAll('.testimonial')].forEach( el => { el.style.color = 'red'; });

提供的类似数组的集合

Element.querySelectorAll()
[...document.querySelectorAll('.testimonial')].forEach( el => {
  el.style.color = 'red';
  const stuff = `Element ${el.tagName} with ID #${el.id} says: ${el.textContent}`; 
  console.log( stuff );
});

答案 14 :(得分:0)

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

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