0

すべての画像を 1 枚ずつ数えたいと思います<div class="thumbnail">

私のコード:

    <div class="thumbnail">     

    <a rel="first" href="#">
        <div>
        <div></div>
        <img src="...">
        </div>
    </a>

    <div class="hidden-fb"> <!-- // CSS display: none -->                                                                                                 
        <a rel="first" href="#">
            <div>
            <div></div>
            <img src="...">
            </div>
        </a>                                               
    </div><!-- .hidden-fb -->

</div><!-- .thumbnail -->

<h2>Contains 2* images </h2>

私はこれを試します

var featureImageCount = $('div.thumbnail img').length;    
$('div.thumbnail').after('<div><h1>' + featureImageCount + ' Images</h1></div>');

しかし、私はすべての div "(6)" だけを取得し
ます jquery でこれを行うことはできますか?
または、jquery はさまざまなrel属性でアイテムをカウントできますか?

jsフィドル

ありがとう
オグニ

4

2 に答える 2

2

.thumbnailを使用して、各要素を個別に調べる必要があります.each。次に、$(this)コールバックで を使用して実際の要素にアクセスできます。そこから、すべての特定の子孫<img>要素を見つけることができます。そこから、あなたが.afterしようとするように、$(this).

$(document).ready(function () {
    $("div.thumbnail").each(function () {
        var $this = $(this),
            count = $this.children("a").children("img").length;
        $this.after("<div><h1>" + count + " Images</h1></div>");
    });
});

http://jsfiddle.net/yCazz/8/

于 2013-02-26T18:46:53.973 に答える
0
$('div.thumbnail').each(function () {

    var $this = $(this),
        count = $this.find('img').length;

    $this.after('<h2>', {
        text: 'Contains (' + count  + '*) image' + (count == 1 ? '' : 's')
    });

});
于 2013-02-26T18:48:06.547 に答える