我試圖獲取存儲在每個帖子的post-content
容器中的圖像數量。獲取特定div中的圖像數量
後的佈局是這樣的:
<div class="post">
<div class="post-toolbar">
<div class="post-date">date</div>
<div class="signs">
<div class="hearts">♥</div>
<div><img src="logo.png"></div>
<div><img src="logo2.png"></div>
</div>
<div class="post-title">title</div>
</div>
<div class="post-content">
<a href="#link"><img src="image.png"></a>
<a href="#link"><img src="image.png"></a>
</div>
</div>
而且一段JavaScript代碼,它看起來像這樣:
$('.hearts').live("click",function() {
var amount = $(this).parent().parent().parent().find("img").size();
console.log(amount);
});
目前的amount
值是4
。
我敢肯定這是一個更好的方式來訪問與jQuery .post-content
股利。
$(「img」,「div.post-content」)可能會有幫助。 jQuery讓你有機會使用上下文(第二參數)來搜索你的內容(第一個參數)。 – microspino 2012-01-02 03:16:46
@microspino將獲得所有'後內容'div的圖像,而不是每個帖子。 – PeeHaa 2012-01-02 03:18:51