Javascript jQuery循环使用相同类的元素

Javascript jQuery循环使用相同类的元素,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,我有大量的div类,我想使用jquery循环检查它们,以检查每个div的特定条件是否为true。如果为真,则应执行操作 有人知道我会怎么做吗?试试这个 $('.testimonial').each(function(){ //if statement here // use $(this) to reference the current div in the loop //you can try something like... if(condition

我有大量的div类,我想使用jquery循环检查它们,以检查每个div的特定条件是否为true。如果为真,则应执行操作

有人知道我会怎么做吗?

试试这个

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


    if(condition){

    }


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

查看以了解更多信息。

您可以这样做

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

我可能遗漏了问题的一部分,但我相信你可以这么做:

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

这使用jQuery的each方法:

您可以使用以下方法简洁地执行此操作。以下示例将隐藏包含单词something的所有

$(".testimonial").filter(function() {
    return $(this).text().toLowerCase().indexOf("something") !== -1;
}).hide();
可以帮助您使用索引方法遍历元素

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

现在在没有jQuery的情况下实现这一点非常简单

没有jQuery: 只需选择元素并使用以下命令对其进行迭代:

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

更准确地说:

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

使用一个简单的for循环:

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

document.querySelectorAll.commential.forEachfunction元素,索引{ element.innerHTML='推荐'+索引+1; }; 在JavaScript ES6中 在类似Element.querySelectorAll给定的数组上

document.querySelectorAll.commential.forEach el=>{ el.style.color='红色'; console.log`Element${el.tagName}和ID${el.ID}表示:${el.textContent}`; }; 这是一些文字


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

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

带有i、obj参数的函数帮助很大。如果只使用了每一个,那么它不是迭代的。@Darvindeeds是正确的!实际迭代器使用该函数来处理每个项。返回false将停止迭代。值得指出的是,obj将是dom对象,而$this是jQuery对象。我们不能执行jQuerythis“ul li”。length获取元素ul li的长度?+1建议$this访问对象。。。作为DOM对象的obj不允许直接附加函数,例如obj.emptythat不提供jquery对象,只提供DOMelements@celwell不能指望jQuery为您做任何事情。这是一个制作您自己的jQuery对象$ind的问题。这确实是最有效的方法。如果您喜欢从更实用的角度阅读/写作,这很好。这里已经有了几乎相同的答案,我认为您应该编辑现有的fyi:break;不会破裂。您必须使用return false;不需要spread运算符+数组表示法,当然执行doc..torAll.forEach就足够了?谢谢。绝对地[…ArrayLike]在querySelectorAll不支持.forEach时使用@亚伯史密斯
$('div[class="testimonial"]').each(function(index,item){
    if(parseInt($(item).data('index'))>2){
        $(item).html('Testimonial '+(index+1)+' by each loop');
    }
});
$.each($('.testimonal'), function(index, value) { 
    console.log(index + ':' + value); 
});
var testimonials= $('.testimonial');
for (var i = 0; i < testimonials.length; i++) {
  // Using $() to re-wrap the element.
  $(testimonials[i]).text('a');
}
$.each($('.testimonal'), function(i, val) { 
    if(your condition){
       //your action
    }
});