1. DOMdocument object model的缩写 文档对象模型
  2. DOM是关于如何获取、修改、添加和删除HTML元素的标准
  3. DOM是由节点组成的
  4. 节点的关系:父子兄弟

1、获取节点的方法

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">app</div>
  11. <ul>
  12. <li class="item">1</li>
  13. <li class="item">2</li>
  14. <li class="item">3</li>
  15. <li class="item">4</li>
  16. </ul>
  17. <script>
  18. var $app=document.querySelector('#app');
  19. console.log($app);
  20. var $item=document.querySelector('.item');
  21. console.log($item);
  22. var $items=document.querySelectorAll('.item');
  23. console.log($items);
  24. </script>
  25. </body>
  26. </html>

2、获取样式和属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. a{background-color: black;}
  10. </style>
  11. </head>
  12. <body>
  13. <a href="https://www.qq.com" style="font-size: 20px;color: aqua;">QQ</a>
  14. <script>
  15. //获取a的href属性
  16. var $a = document.querySelector('a');
  17. console.log($a.href);
  18. //获取a的样式
  19. $a.style.color = 'red';
  20. console.log('a的样式:', $a.style.fontSize, $a.style.color);
  21. console.log('');
  22. </script>
  23. </body>
  24. </html>

3、获取文本和标签

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <div>
  12. aaaaaa
  13. <p>
  14. bbbbbb
  15. </p>
  16. </div>
  17. </div>
  18. <script>
  19. var $app = document.querySelector('#app');
  20. console.log($app.innerHTML);
  21. console.log($app.innerText);
  22. </script>
  23. </body>
  24. </html>

4、添加节点

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <ul id="list">
  11. <li>1</li>
  12. <li>2</li>
  13. <li>3</li>
  14. </ul>
  15. <script>
  16. //创建节点
  17. var $li=document.createElement('li');
  18. $li.innerText=4;
  19. //把li节点插入到ul里面
  20. var $list = document.querySelector('#list')
  21. $list.appendChild($li);
  22. </script>
  23. </body>
  24. </html>

5、通过事件添加节点

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <button onclick="add()">添加节点</button>
  11. <ul id="list">
  12. <li>1</li>
  13. <li>2</li>
  14. <li>3</li>
  15. </ul>
  16. <script>
  17. var num = 4;
  18. function add() {
  19. //创建节点
  20. var $li = document.createElement('li');
  21. $li.innerText = num;
  22. $li.innerText = num++;
  23. //把li节点插入到ul里面
  24. var $list = document.querySelector('#list')
  25. $list.appendChild($li);
  26. }
  27. </script>
  28. </body>
  29. </html>

6、删除节点

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <button onclick="remove()">删除节点</button>
  11. <ul id="list">
  12. <li>1</li>
  13. <li>2</li>
  14. <li>3</li>
  15. </ul>
  16. <script>
  17. var num = 4;
  18. function remove() {
  19. var $list = document.querySelector('#list');
  20. $list.remove();
  21. }
  22. </script>
  23. </body>
  24. </html>

7、修改节点样式属性(1)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. div {
  10. border: 1px solid;
  11. margin-top: 20px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <button onclick="showDiv()">显示</button>
  17. <div style="display: none;" class="box">
  18. <p>xxxxxx</p>
  19. <p>xxxxxx</p>
  20. <p>xxxxxx</p>
  21. </div>
  22. <script>
  23. function showDiv() {
  24. var $box=document.querySelector('.box');
  25. var $button=document.querySelector('button');
  26. var flag = $box.style.display;
  27. if(flag=='none'){
  28. $box.style.display='block';
  29. $button.innerText='隐藏';
  30. }else {
  31. $box.style.display='none';
  32. $button.innerText='显示';
  33. }
  34. }
  35. </script>
  36. </body>
  37. </html>

8、修改节点样式属性(2)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. div {
  10. border: 1px solid;
  11. margin-top: 20px;
  12. }
  13. .dsn {
  14. display: none;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <button onclick="showDiv()">显示</button>
  20. <div class="box">
  21. <p>xxxxxx</p>
  22. <p>xxxxxx</p>
  23. <p>xxxxxx</p>
  24. </div>
  25. <script>
  26. function showDiv() {
  27. var $box = document.querySelector('.box');
  28. var $button = document.querySelector('button');
  29. var className = $box.className;
  30. if (className == 'box') {
  31. $box.className = 'box dsn';
  32. $button.innerText = '隐藏';
  33. } else {
  34. $box.className = 'box';
  35. $button.innerText = '显示';
  36. }
  37. }
  38. </script>
  39. </body>
  40. </html>

9、修改节点元素属性

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. .box {
  10. width: 300px;
  11. height: 300px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <button onclick="changeImg(1)">换图1</button>
  17. <button onclick="changeImg(2)">换图2</button>
  18. <div class="box">
  19. <img src="./img/fj1.jpg" alt="">
  20. </div>
  21. <script>
  22. function changeImg(num){
  23. document.querySelector('img').src=`./img/fj${num}.jpg`;
  24. }
  25. </script>
  26. </body>
  27. </html>