前端一般做电商或者统计数据的时候需要让用户可以直观的看到有多少数据 , 这时候就需要有这种共计的功能了 , 废话不多说 , 上源码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>jquery获取一个div中有多少个子div</title>
<!-- 引入在线jquery插件库 -->
<script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script>
</head>
<body>
<div class="box">
<div>1</div>
<div>1</div>
<div>1</div>
<div>1</div>
</div>
<div class="box1">
共有<span></span>个div
</div>
<script type="text/javascript">
$(function(){
// 1、获取box下的div的个数,放到number里(两种方法)
// (1) var number = $('.box div').length
// (2) var number = $('.box div').size()
// 2、将number显示到span里
var number = $('.box div').length
$('.box1 span').text(number)
})
</script>
</body>
</html>