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>