2017-09-15 4 views
-3
私は常にGoogle ChromeのコンソールでのjQueryを経由してページを更新するには、画像をカウントしようとしている

私は次のスクリプトを書きましたが、動作していないようにjQuery

を使用して絶えず更新するページ上の画像を数えます適切に、あなたは私を助けることができれば、私は喜んでいるでしょう:

var count = 0; 
window.setInterval(function() { 
    $('.image .big').each(function(i, obj) { 
    if ($('.box').find('img').attr("src") == "special-src/img.png") { 
     count++ 
    } else { 
     count-- 
    }; 
    $('.image .big').addClass('.done').removeClass('.image .big'); // To not count it multiple times 
    console.log(count); 
    }); 
}, 1000); 

編集:私はそのようなルックスで働いている HTML: (divのはcontantly除去され、追加されている)

<div class="box"> 
     <img src="special-src/img.png" class="image big"/> 
</div> 
<div class="box"> 
     <img src="special-src/img2.png" class="image big"/> 
</div> 
+1

誰もがあなたが、私が見 –

+0

@RoryMcCrossan、で作業しているHTMLを見ずにここであなたを助けるためには不可能になるだろう、私は今、そのOKならば、あなたが私に言うことができる質問を編集しましたか? –

+0

明確にするために、間隔の中で 'src =" special-src/img.png "'ですべての 'img'要素を数え、それぞれの画像を1度だけ数えようとしています。そうですか? –

答えて

0

いくつかのクリエイティブなCSSセレクタで、カウントしたい要素を見つけることができます。各セレクターが何をしているかを明確にするためにいくつかのコメントを追加しました。答えは、特別な画像が3つ、通常の画像が1つ追加されているので、番号2を出力する必要があります。

(function() { 
 
    var 
 
    count = 0, 
 
    // Selector for finding all items with the class "box" but with the class "done". 
 
    selectorNotProcessed = '.box:not(.done)', 
 
    // Selector for all element with the classes "image" and "big" with a "src" attribute 
 
    // that contains "img1.png" and that have an ancestor with the class "box" but not 
 
    // the class "done" 
 
    selectorSpecialNotDone = `${selectorNotProcessed} .image.big[src*="img1.png"]`, 
 
    // Selector for all element with the classes "image" and "big" with a "src" attribute 
 
    // that DOESN'T contain "img1.png" and that have an ancestor with the class "box" but 
 
    // not the class "done"  
 
    selectorNotSpecialNotDone = `${selectorNotProcessed} .image.big:not([src*="img1.png"])`; 
 
    
 
    setInterval(function() { 
 
    var 
 
     // Get all the non processed special src elements. 
 
     specialElements = document.querySelectorAll(selectorSpecialNotDone), 
 
     // Get all the non processed non-special src elements. 
 
     notSpecialElements = document.querySelectorAll(selectorNotSpecialNotDone); 
 
     
 
    // Update the count of the number of special src elements vs non-special src elements 
 
    count += (specialElements.length - notSpecialElements.length); 
 
    
 
    // Add the done class to all the elements with the class "box" that do not yet have the 
 
    // class "done". 
 
    document.querySelectorAll(selectorNotProcessed).forEach(element => element.classList.add('done')); 
 
    
 
    console.log(count); 
 
    }, 1000); 
 
})();
<div class="box"> 
 
    <img src="folder/img1.png" class="image big"/> 
 
</div> 
 
<div class="box"> 
 
    <img src="folder/img2.png" class="image big"/> 
 
</div> 
 
<div class="box"> 
 
    <img src="folder/img1.png" class="image big"/> 
 
</div> 
 
<div class="box"> 
 
    <img src="folder/img1.png" class="image big"/> 
 
</div>

+0

それは私が必要としているほとんどのようですが、私は正しく自分自身を説明しなかったので、スクリプトは何をすべきか、img.pngは+1、img2.pngは-1、彼らは同じフォルダにあります(特別なsrcはありません) 編集:実際にあなたのスクリプトは私が必要とするものを正確に実行していることがわかります。画像は同じ場所になければなりません。唯一の違いは、名前 –

+0

私は答えを更新しました。 'img1.png'を使ってすべてバランスを増やし、他のすべてがバランスを減らします。 – Thijs

+0

答えは正しく、ここで作業しているようですが、私のコンソールで "未定義"というメッセージが表示され、.boxがそれに追加されます。 –

0

チェックアウトイメージを動的にカウントするための例を扱うため、このlink

$(document).bind('DOMSubtreeModified', function() { 

    console.log("document changed"); 
    if (totalImages !== $(".image.big").length && !disableRecounting) { 
     disableRecounting = true; 
     console.log("recount images..", totalImages, $(".image.big").length); 
     countImages(); 
    } 
}); 
関連する問題