获取特定 div 中的图像数量

发布于 2024-12-24 00:59:29 字数 1004 浏览 5 评论 0原文

我试图获取存储在每个帖子的 post-content 容器中的图像数量。

帖子的布局如下所示:

<div class="post">
  <div class="post-toolbar">
    <div class="post-date">date</div>
    <div class="signs">
      <div class="hearts">&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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2024-12-31 00:59:29
$('.hearts').live("click",function() {
    var post = $(this).closest('.post'); // will find the first element with the class post up the DOM tree
    var amount = $('.post-content img', post).size();
    console.log(amount);        
});

顺便说一句,你应该真正研究 .delegate() 并且永远不要使用 .live() 再次,因为 live 慢得多比代表。

如果您使用的是 jQuery 1.7 或更高版本,则更好的是使用 .on()

$('.hearts').live("click",function() {
    var post = $(this).closest('.post'); // will find the first element with the class post up the DOM tree
    var amount = $('.post-content img', post).size();
    console.log(amount);        
});

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().

吃兔兔 2024-12-31 00:59:29

$(this).parents(".post") 怎么样?

$('.hearts').live("click",function() {
    var amount = $(this).parents(".post").children(".post-content").find("img").size();
    alert(amount);       
});

How about $(this).parents(".post") ?

$('.hearts').live("click",function() {
    var amount = $(this).parents(".post").children(".post-content").find("img").size();
    alert(amount);       
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文