geo*_*310 536 javascript jquery jquery-selectors
我有一个div加载类testimonial
,我想使用jquery循环它们来检查每个div,如果特定条件为真.如果是,它应该执行一个动作.
有谁知道我会怎么做?
Kee*_*ker 972
使用each:' i
'是数组中的位置,obj
是您正在迭代的DOM对象(也可以通过jQuery包装器访问$(this)
).
$('.testimonial').each(function(i, obj) {
//test
});
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请查看api参考.
ste*_*776 115
试试这个...
$('.testimonial').each(function(){
//if statement here
// use $(this) to reference the current div in the loop
//you can try something like...
if(condition){
}
});
Run Code Online (Sandbox Code Playgroud)
Jos*_*ier 50
这些天没有jQuery这样做很简单.
只需选择元素并使用该.forEach()
方法迭代它们:
var testimonials = document.querySelectorAll('.testimonial');
Array.prototype.forEach.call(testimonials, function(elements, index) {
// conditional here.. access elements
});
Run Code Online (Sandbox Code Playgroud)
Man*_*noj 40
试试这个例子
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>
Run Code Online (Sandbox Code Playgroud)
当我们想访问那些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');
}
});
Run Code Online (Sandbox Code Playgroud)
Ghy*_*hal 27
你可以这样做
$('.testimonial').each(function(index, obj){
//you can use this to access the current item
});
Run Code Online (Sandbox Code Playgroud)
iko*_*tia 17
divs = $('.testimonial')
for(ind in divs){
div = divs[ind];
//do whatever you want
}
Run Code Online (Sandbox Code Playgroud)
Ath*_*rva 15
jQuery的.eq()可以帮助您使用索引方法遍历元素.
var testimonialElements = $(".testimonial");
for(var i=0; i<testimonialElements.length; i++){
var element = testimonialElements.eq(i);
//do something with element
}
Run Code Online (Sandbox Code Playgroud)
kar*_*m79 14
你可以简明扼要地使用它.filter
.以下示例将隐藏包含单词"something"的所有.testimonial div:
$(".testimonial").filter(function() {
return $(this).text().toLowerCase().indexOf("something") !== -1;
}).hide();
Run Code Online (Sandbox Code Playgroud)
Ism*_*bad 10
使用简单的for循环:
var testimonials= $('.testimonial');
for (var i = 0; i < testimonials.length; i++) {
// Using $() to re-wrap the element.
$(testimonials[i]).text('a');
}
Run Code Online (Sandbox Code Playgroud)
小智 10
您可以使用 jQuery $each 方法循环遍历所有具有类推荐的元素。i => 是集合中元素的索引,val 为您提供该特定元素的对象,您可以使用“val”进一步访问元素的属性并检查您的条件。
$.each($('.testimonal'), function(i, val) {
if(your condition){
//your action
}
});
Run Code Online (Sandbox Code Playgroud)
我可能会遗漏部分问题,但我相信你可以这样做:
$('.testimonial').each((index, element) => {
if (/* Condition */) {
// Do Something
}
});
Run Code Online (Sandbox Code Playgroud)
在 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}` );
});
Run Code Online (Sandbox Code Playgroud)
<p class="testimonial" id="1">This is some text</p>
<div class="testimonial" id="2">Lorem ipsum</div>
Run Code Online (Sandbox Code Playgroud)
小智 7
没有jQuery更新
document.querySelectorAll('.testimonial').forEach(function (element, index) {
element.innerHTML = 'Testimonial ' + (index + 1);
});
Run Code Online (Sandbox Code Playgroud)
<div class="testimonial"></div>
<div class="testimonial"></div>
Run Code Online (Sandbox Code Playgroud)
$('.testimonal').each(function(i,v){
if (condition) {
doSomething();
}
});
Run Code Online (Sandbox Code Playgroud)