클래스에 div가 많이 있으며 testimonialjquery를 사용하여 특정 조건이 true 인 경우 각 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>우리가 접근하는 사람들하려는 경우 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');
    }
});답변
이 방법으로 할 수 있습니다
$('.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
}