博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery循环使用相同类的元素
阅读量:2289 次
发布时间:2019-05-09

本文共 1629 字,大约阅读时间需要 5 分钟。

本文翻译自:

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. 我有一个带有类testimonial的div的加载,我想使用jquery循环遍历它们以检查每个div,如果特定条件为真。 If it is true, it should perform an action. 如果是,它应该执行一个动作。

Does anyone know how I would do this? 有谁知道我会怎么做?


#1楼

参考:


#2楼

can help you traverse through elements with an indexed approach. 可以帮助您使用索引方法遍历元素。

var testimonialElements = $(".testimonial");for(var i=0; i

#3楼

It's pretty simple to do this without jQuery these days. 这些天没有jQuery这样做很简单。

Without jQuery: 没有jQuery:

Just select the elements and use the to iterate over them: 只需选择元素并使用迭代它们:

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

#4楼

Try this example 试试这个例子

Html HTML

Testimonial 1
Testimonial 2
Testimonial 3
Testimonial 4
Testimonial 5

When we want to access those divs which has data-index greater than 2 then we need this jquery. 当我们想要访问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');    }});


#5楼

More precise: 更确切:

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

#6楼

With a simple for loop: 使用简单的for循环:

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

转载地址:http://bscnb.baihongyu.com/

你可能感兴趣的文章
psql 常用命令
查看>>
postgresql 中表的相关操作
查看>>
postgres 权限相关操作
查看>>
postgres 行安全策略
查看>>
postgres 事务
查看>>
postgres 窗口函数
查看>>
postgres 模式schema
查看>>
postgres 继承 Inheritance
查看>>
postgres 分区表
查看>>
postgres 从修改行返回数据
查看>>
postgres 查询命令详解
查看>>
postgres 索引
查看>>
postgres 并发控制
查看>>
postgres 性能优化小技巧
查看>>
Bash 脚本基本语法
查看>>
linux 用户和组管理命令
查看>>
X509 证书详解
查看>>
nginx 概念和基本功能
查看>>
nignx SSL 管理详解
查看>>
Google 搜索常用命令和小技巧
查看>>