簡體   English   中英

jQuery 遍歷具有相同類的元素

[英]jQuery to loop through elements with the same class

我有很多帶有類testimonial的 div,我想使用 jquery 循環遍歷它們以檢查每個 div 是否滿足特定條件。 如果是真的,它應該執行一個動作。

有誰知道我會怎么做?

使用每個: ' i ' 是數組中的位置, obj是您正在迭代的 DOM 對象(也可以通過 jQuery 包裝器$(this)訪問)。

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

查看api 參考以獲取更多信息。

試試這個...

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


    if(condition){

    }


 });

現在不用 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大於2 divs ,我們需要這個 jquery。

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

工作示例小提琴

你可以這樣做

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

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

var testimonialElements = $(".testimonial");
for(var i=0; i<testimonialElements.length; i++){
    var element = testimonialElements.eq(i);
    //do something with element
}
divs  = $('.testimonial')
for(ind in divs){
  div = divs[ind];
  //do whatever you want
}

您可以使用.filter簡潔地做到這.filter 以下示例將隱藏所有包含單詞“something”的 .testimonial div:

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

用一個簡單的 for 循環:

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

我可能遺漏了問題的一部分,但我相信你可以簡單地這樣做:

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

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

沒有 jQuery 更新

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

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

更精確:

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

在 JavaScript ES6 .forEach() 中,通過Element.querySelectorAll()給出的類似數組的NodeList集合

 document.querySelectorAll('.testimonial').forEach( el => { el.style.color = 'red'; console.log( `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
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM