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