2019/01/30

jQueryでエリア内のある要素の数を合計する









ページ全体ではなく、ある要素内の指定の要素の数を数えて表示するjs。
eachでその中をまわして、lengthでとった数を合計して表示しています。




sample

See the Pen エリア内のある要素の数を合計する by takapen (@takapen) on CodePen.








html

aaa

  • aaa

  • ccc

  • aaa

  • bbb

aaa

  • aaa

  • ccc

  • aaa

  • bbb







jQuery
$(function(){
  $('section').each(function(){
    var soldout = $(this).find('.soldout').length;
    var soldoutTest = $(this).find('.soldout-test').length;
    var sum = soldout + soldoutTest;
    $(this).find('span').text('('+sum+')');
  });
});