jQuery 循环遍历具有相同类的元素

发布于 2024-10-12 15:11:30 字数 112 浏览 6 评论 0原文

我有很多带有 testimonial 类的 div,我想使用 jquery 循环它们来检查每个 div 是否满足特定条件。如果为真,则应执行操作。

有谁知道我会怎么做?

I have a load of divs with the class testimonial and 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.

Does anyone know how I would do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

杀手六號 2024-10-19 15:11:30

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

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

有关详细信息,请查看 API 参考

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

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

Check the api reference for more information.

仅冇旳回忆 2024-10-19 15:11:30

试试这个...

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


    if(condition){

    }


 });

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){

    }


 });
像极了他 2024-10-19 15:11:30

如今,无需 jQuery 即可轻松完成此操作。

没有 jQuery:

只需选择元素并使用 < code>.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
});

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

Without jQuery:

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

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
});
萌吟 2024-10-19 15:11:30

试试这个例子

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大于2div时,我们需要这个jquery.

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

小提琴工作示例

Try this example

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 divs which has data-index greater than 2 then we need this 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

江南烟雨〆相思醉 2024-10-19 15:11:30

你可以这样做

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

you can do it this way

$('.testimonial').each(function(index, obj){
    //you can use this to access the current item
});
顾北清歌寒 2024-10-19 15:11:30

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

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

这使用 jQuery 的 every 方法: https://learn.jquery.com/using-jquery-core/iteating/

I may be missing part of the question, but I believe you can simply do this:

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

This uses jQuery's each method: https://learn.jquery.com/using-jquery-core/iterating/

壹場煙雨 2024-10-19 15:11:30

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

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

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

var testimonialElements = $(".testimonial");
for(var i=0; i<testimonialElements.length; i++){
    var element = testimonialElements.eq(i);
    //do something with element
}
一个人的旅程 2024-10-19 15:11:30
divs  = $('.testimonial')
for(ind in divs){
  div = divs[ind];
  //do whatever you want
}
divs  = $('.testimonial')
for(ind in divs){
  div = divs[ind];
  //do whatever you want
}
清君侧 2024-10-19 15:11:30

用一个简单的 for 循环:

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

With a simple for loop:

var testimonials= $('.testimonial');
for (var i = 0; i < testimonials.length; i++) {
  // Using $() to re-wrap the element.
  $(testimonials[i]).text('a');
}
成熟稳重的好男人 2024-10-19 15:11:30

您可以使用 .filter 简洁地完成此操作。以下示例将隐藏所有包含单词“something”的 .testimonial div:

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

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

$(".testimonial").filter(function() {
    return $(this).text().toLowerCase().indexOf("something") !== -1;
}).hide();
转瞬即逝 2024-10-19 15:11:30

没有更新 jQuery

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

Without jQuery updated

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

怕倦 2024-10-19 15:11:30

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

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

You could use the jQuery $each method to loop through all the elements with class testimonial.
i => is the index of the element in collection and val gives you the object of that particular element and you can use "val" to further access the properties of your element and check your condition.

$.each($('.testimonal'), function(i, val) { 
    if(your condition){
       //your action
    }
});
瀞厅☆埖开 2024-10-19 15:11:30

在 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}` );
});
<p class="testimonial" id="1">This is some text</p>
<div class="testimonial" id="2">Lorem ipsum</div>

In JavaScript ES6 .forEach()
over an array-like NodeList collection given by 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}` );
});
<p class="testimonial" id="1">This is some text</p>
<div class="testimonial" id="2">Lorem ipsum</div>

迷爱 2024-10-19 15:11:30
$('.testimonal').each(function(i,v){
  if (condition) {
    doSomething();
  }
});
$('.testimonal').each(function(i,v){
  if (condition) {
    doSomething();
  }
});
夜空下最亮的亮点 2024-10-19 15:11:30

更精确:

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

More precise:

$.each($('.testimonal'), function(index, value) { 
    console.log(index + ':' + value); 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文