Web APIs是js基礎的應用,大量使用js基礎作頁面交互效果
DOM:文檔對象模型,提供訪問與操做網頁內容的接口與方法。經過DOM,能夠改變網頁的結構與外觀與樣式
以上內容均可以看做對象javascript
經過getElementById(id)獲取
注意事項html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div id="box">hello </div> <script> var box = document.getElementById('box'); console.log(box) //打印元素對象 console.log(typeof box) //查看元素對象類型 console.dir(box) </script> </body> </html>
根據getElementsByTagName('標籤名')獲取
注意事項
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <ul> <li>WEB前段自學者</li> <li>WEB前段自學者</li> <li>WEB前段自學者</li> <li>WEB前段自學者</li> <li>WEB前段自學者</li> <li>WEB前段自學者</li> </ul> <script> var lis = document.getElementsByTagName('li'); // 經過標籤名來獲取元素 console.log(lis) //打印元素對象 for (var i = 0; i < lis.length; i++) { console.log(lis[i]) //經過遍歷獲取每個元素對象 } </script> </body> </html>
思路前端
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <!-- 要求:獲取第二個ul裏面的li標籤 --> <ul> <li>WEB前端</li> <li>WEB前端</li> <li>WEB前端</li> <li>WEB前端</li> <li>WEB前端</li> <li>WEB前端</li> </ul> <ul id="uls"> <li>hello</li> <li>hello</li> <li>hello</li> <li>hello</li> <li>hello</li> <li>hello</li> <li>hello</li> <li>hello</li> </ul> <script> var uls = document.getElementById('uls'); var lis = uls.getElementsByTagName('li'); console.log(lis) </script> </body> </html>
getElementsByClassName('類名')
注意事項html5
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div class="box">盒子</div> <div class="box">盒子</div> <script> var boxs = document.getElementsByClassName('box'); console.log(boxs) </script> </body> </html>
注意事項java
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <div class="boxs">盒子1</div> <div class="boxs">盒子2</div> <script> var boxs = document.querySelector('.boxs'); console.log(boxs) //獲取的是指定選擇器的第一個元素對象 </script> </body> </html>
注意事項node
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <ul class="box"> <li>hello</li> <li>hello</li> <li>hello</li> <li>hello</li> </ul> <script> var boxs = document.querySelectorAll('.box'); console.log(boxs); var lis = document.querySelectorAll('li'); console.log(lis) </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> var body = document.body; console.log(body) console.dir(body) var html = document.documentElement; console.log(html) console.dir(html) </script> </body> </html>