jQuery: comment aborder un sous-article avec une classe spéciale
-
27-10-2019 - |
Question
Je veux itérer sur la ligne éléments. Chaque ligne élément a élément éléments comme éléments enfants. Alors d'abord je itérer sur ligne et je veux aborder un certain élément élément par exemple le troisième. Voici le code que j'ai:
$(".pricetable .righttable .line:not(.blank)").each(function(j) {
var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);
currentLine.(".item").each(function(k) {
$(".pricetable .righttable .line .item").eq(i).addClass("active");
});
});
Le problème est que je ne sais pas comment aborder l'élément enfant avec élément classe.
currentLine.(".item").each(function(k)
Cela ne fonctionne pas.
filtre XML est appliqué à la valeur non-XML ({0: n ° 2 = ({}), longueur: 1, prevObject: {longueur: 3, prevObject: {0: n ° 1 = ({}), le contexte: # 1 #, longueur: 1}, le contexte: # 1 #, sélecteur: ".righttable .line Aperçu de prix:. pas (.blank)", 0: # 2 # 1: ({}), 2: ({} )}, contexte: # 1 #, sélecteur: ".righttable .line Aperçu de prix: non (.blank) .slice (0,1)"}) file: /// C: /Users/xxx/lib/pricetable.js Ligne 112
Edit: Wow, je ne pensais que je reçois une bonne et une réponse rapide! Je pense que je vais avec cette solution:
$(".pricetable .righttable .line:not(.blank)").each(function(j) {
$(this).children(".item").eq(i).addClass("active");
});
La solution
$(".pricetable .righttable .line:not(.blank)").each(function(j) {
var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);
currentLine.find(".item").each(function(k) {
$(".pricetable .righttable .line .item").eq(i).addClass("active");
});
});
If they're direct children of currentLine, you should just be able to do children('.item')
in place of find('.item')
. This is called Traversing the DOM. Visit this page (http://api.jquery.com/category/traversing/) and read through the descriptions of some of the functions there. They'll be very useful to keep in mind if you traverse a lot :)
Autres conseils
$('.pricetable .righttable .line:not(.blank)').find('.item').each(function() {
// this point to child element - item
// use $(this).addClass('active');
});
Slight improvement before I answer your question:
var currentLine = $(".pricetable .righttable .line:not(.blank)").eq(j);
is easily replaceable with:
var currentLine = $(this);
Second, if you wish to find the 3rd .item
element inside of $(this)
, use .find()
.find(".item:nth-child(3)");
No need to iterate over the lines. If you must iterate over the lines, then just compare k
to 2
(As JavaScript counts from 0) and that's it.
If you want to do something with the nth item
of each line
, it's much easier than it looks:
var index = 3; // the third "item" element
$(".pricetable .righttable .line:not(.blank) .item:nth-child(" + index + ")")
.addClass("active");
Is there something else you want to do that the above is not sufficient for?
$(".pricetable .righttable .line:not(.blank)").each(function() {
$(this).find('.item:nth-child(2)').addClass('active');
});