jQuery循环使用相同类的元素

geo*_*310 536 javascript jquery jquery-selectors

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

有谁知道我会怎么做?

Kee*_*ker 972

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

$('.testimonial').each(function(i, obj) {
    //test
});
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看api参考.

  • 值得指出的是,"obj"将是dom对象,而$(this)是jQuery对象. (121认同)
  • +1表示`$(this)`来访问对象...`obj`是DOM对象不允许直接附加函数,例如`obj.empty()` (14认同)
  • 使用i,obj参数的功能有很大帮助.如果只使用了每个,那么它就不是迭代的. (2认同)
  • @Darwindeeds正确!实际的迭代器使用该函数来处理每个项目。返回“ false”将停止迭代。 (2认同)

ste*_*776 115

试试这个...

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


    if(condition){

    }


 });
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考:'休息;`不会破坏.你必须使用`return false;` (2认同)

Jos*_*ier 50

这些天没有jQuery这样做很简单.

没有jQuery:

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

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


Man*_*noj 40

试试这个例子

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>
Run Code Online (Sandbox Code Playgroud)

当我们想访问那些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');
    }
});
Run Code Online (Sandbox Code Playgroud)

工作范例小提琴


Ghy*_*hal 27

你可以这样做

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


iko*_*tia 17

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

  • @celwell 不能指望 jQuery 为您做所有事情。这是创建您自己的 jQuery 对象“$(ind)”的问题。 (2认同)

Ath*_*rva 15

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

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


kar*_*m79 14

你可以简明扼要地使用它.filter.以下示例将隐藏包含单词"something"的所有.testimonial div:

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


Ism*_*bad 10

使用简单的for循环:

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


小智 10

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

$.each($('.testimonal'), function(i, val) { 
    if(your condition){
       //your action
    }
});
Run Code Online (Sandbox Code Playgroud)


Dav*_*ith 8

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

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


Rok*_*jan 8

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

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


小智 7

没有jQuery更新

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


dav*_*vin 6

$('.testimonal').each(function(i,v){
  if (condition) {
    doSomething();
  }
});
Run Code Online (Sandbox Code Playgroud)