この質問の言い方がわかりません... n 番目の要素ごとに each ループを実行するスクリプトがあります。
これを変数として持っています。var nthCard = $('.card:nth-child(' + col + 'n+' + i + ')');
次に、それを使用して .each を実行しますnthCard.each(function(i){...}
nthCards
each の後、 classを持つ の数を調べる必要があります.featured
。
html は次のように設定されています。
<div class="card"></div>
<div class="card featured"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card featured"></div>
これが各ステートメントであると仮定すると、これらのカードのうち何枚がフィーチャーされているかを見つける必要があります。
私はこのようなことを試しました:console.log(nthCard.find('.featured').length);
またconsole.log(nthCard.hasClass('.featured').length);
、前者は戻り、後者は0
戻りますundefined
。
切り捨てられたコードは次のとおりです。
function placeCard(col){
var i = 0;
for(i=1; i<= col; i++){
var nthCard = $('.card:nth-child(' + col + 'n+' + i + ')');
nthCard.each(function(idx){
//do some code
});
console.log(nthCard.find('.featured').length);//I need to return the number of featured cards here.
}
}