Javascript jQuery 遍历具有相同类的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4735342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:50:30  来源:igfitidea点击:

jQuery to loop through elements with the same class

javascriptjqueryjquery-selectors

提问by geoffs3310

I have a load of divs with the class testimonialand I want to use jquery to loop through them to check for each div if a specific condition is true. If it is true, it should perform an action.

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

Does anyone know how I would do this?

有谁知道我会怎么做?

回答by Kees C. Bakker

Use each: 'i' is the postion in the array, objis the DOM object that you are iterating (can be accessed through the jQuery wrapper $(this)as well).

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

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

Check the api referencefor more information.

查看api 参考以获取更多信息。

回答by stephen776

try this...

尝试这个...

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


    if(condition){

    }


 });

回答by Josh Crozier

It's pretty simple to do this without jQuery these days.

现在不用 jQuery 就可以很简单地做到这一点。

Without jQuery:

没有 jQuery:

Just select the elements and use the .forEach()methodto iterate over them:

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

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

In older browsers:

在旧浏览器中:

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

回答by Manoj

Try this example

试试这个例子

Html

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>

When we want to access those divswhich has data-indexgreater than 2then we need this jquery.

当我们想访问那些divs具有data-index大于2那么我们就需要这个jQuery。

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

Working example fiddle

工作示例小提琴

回答by Ghyath Serhal

you can do it this way

你可以这样做

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

回答by Atharva

jQuery's .eq()can help you traverse through elements with an indexed approach.

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

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

回答by ikostia

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

回答by karim79

You can do this concisely using .filter. The following example will hide all .testimonial divs containing the word "something":

您可以使用.filter. 以下示例将隐藏所有包含单词“something”的 .testimonial div:

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

回答by Ismail Rubad

With a simple for loop:

用一个简单的 for 循环:

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

回答by KrisInception

Without jQuery updated

没有 jQuery 更新

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