获取特定 div 中的图像数量
我试图获取存储在每个帖子的 post-content
容器中的图像数量。
帖子的布局如下所示:
<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
div 的更好方法。
I'm trying to get the amount of images which are stored in the post-content
container of each post.
The layout of a post looks like this:
<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>
And a Javascript snippet which looks like this:
$('.hearts').live("click",function() {
var amount = $(this).parent().parent().parent().find("img").size();
console.log(amount);
});
At the moment the value of amount
is 4
.
I'm sure the is a much nicer way to access the .post-content
div with jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
顺便说一句,你应该真正研究
.delegate()
并且永远不要使用.live()
再次,因为 live 慢得多比代表。如果您使用的是 jQuery 1.7 或更高版本,则更好的是使用
.on()
。BTW you should really look into
.delegate()
and never use.live()
again since live is way slower than delegate.Or even better if you are on jQuery 1.7 or higher use
.on()
.$(this).parents(".post")
怎么样?How about
$(this).parents(".post")
?