JS、JQ获取容器内标签的个数


<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取容器内标签的个数</title>
</head>
<body>
<div class="content" id="content">
    <div class="item"><span></span><span></span></div>
    <div class="item"><span></span><span></span></div>
    <div class="item"><span></span><span></span></div>
    <div class="item"><span></span><span></span></div>
    <div class="item"><span></span><span></span></div>
</div>
    <script src="http://www.jq22.com/jquery/jquery-2.1.1.js"></script>
    <script type="text/javascript">
      window.onload = function(){
          var div_l = $(".content").children(".item").length;//JQ获取标签个数方法
        console.log("jq方法:",div_l);

        var div_m = document.getElementById("content");
        var div_n = div_m.getElementsByClassName("item").length;//js获取标签个数方法
        console.log("js方法:",div_n);
      }
    </script>
</body>
</html>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM