我有一个div与类的证明,我想用jquery循环通过他们检查每个div如果一个特定的条件是真的负载。如果为真,它应该执行一个操作。
有人知道我会怎么做吗?
我有一个div与类的证明,我想用jquery循环通过他们检查每个div如果一个特定的条件是真的负载。如果为真,它应该执行一个操作。
有人知道我会怎么做吗?
当前回答
你可以这样做
$('.testimonial').each(function(index, obj){
//you can use this to access the current item
});
其他回答
使用每个:'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){
}
});
使用简单的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的每个方法:https://learn.jquery.com/using-jquery-core/iterating/
你可以使用.filter简单地做到这一点。下面的例子将隐藏所有包含单词"something"的. tribute div:
$(".testimonial").filter(function() {
return $(this).text().toLowerCase().indexOf("something") !== -1;
}).hide();