js獲取dom的方法
1.通過ID獲取 getElementById()
<body> <div id="box"></div> </body> <script> const box=document.getElementById("box") </script>
結果:返回一個dom對象
2.通過類名獲取 getElementsByClassName()
<body> <div class="box"></div> <div class="box"></div> </body> <script> const boxCollection=document.getElemenstByClassName("box") const box1=boxList[0] const box2=boxList[1] </script>
結果:返回一個集合
3.通過name屬性獲取 getElementsByTagName()
<body> <p>1</p> <p>2</p> <p>3</p> <p>4</p> </body> <script> const p=document.getElemenstByTagName("p") </script>
結果:返回一個集合