Dom 对象

目录

  1. DOM 树, DOM 节点, DOM 对象是什么
  2. 利用 DOM 对象对 DOM 节点进行增删改查操作
  3. 事件绑定
  4. 事件常见类型
  5. 事件对象
  6. 事件冒泡和事件捕获

(一) Dom 树, Dom 节点和 Dom 对象

DOM 树 (见图)

domTree.png

2. DOM 节点

3. DOM 对象


4. 全称 Document object Model, 中文名称文档对象模型

作用: 用来的元素节点进行操作, 比如修改元素节点的内容, 修改元素节点的样式

(二) Dom 节点操作(增删改查)

2.1 获取 DOM 节点,获取 DOM 节点的属性,样式和内容

1. 获取 DOM 节点

  1. <body>
  2. <div id="app">app</div>
  3. <ul>
  4. <li class="item">1</li>
  5. <li class="item">2</li>
  6. <li class="item">3</li>
  7. <li class="item">4</li>
  8. <li class="item">5</li>
  9. </ul>
  10. <script>
  11. var $app = document.querySelector('#app');
  12. console.log('app', $app);
  13. var $item = document.querySelector('.item');
  14. console.log('item', $item);
  15. var $items = document.querySelectorAll('.item');
  16. console.log('$items', $items);
  17. </script>
  18. </body>
  1. document.getElementById('xxx'); //根据id来获取dom节点,xxx是id名,id是唯一的
  2. document.querySelector('xxx'); // 根据选择器来获取dom节点(只能获取一个),xxx可以是任意选择器
  3. document.querySelectorAll(); // 根据选择器来获取dom节点(获取全部),xxx可以是任意选择器
  4. dir(xxx) 可以在控制台查看对象的属性

2. 获取 DOM 节点的属性和样式

  1. <style>
  2. a {
  3. color: red;
  4. }
  5. </style>
  6. </head>
  7. <body>
  8. <a href="http://www.baidu.com" style="font-size: 20px;background-color: gray;">百度</a>
  9. <script>
  10. // 获取a标签的href属性
  11. var $a = document.querySelector('a');
  12. console.log('href属性:', $a.href); //
  13. // 获取a标签的样式
  14. console.log('a标签的样式', $a.style.fontSize, $a.style.backgroundColor);
  15. // 获取a标签的字体颜色
  16. console.log('a标签的字体样式', $a.style.color); // 获取不到
  17. console.log('a标签的字体样式', window.getComputedStyle($a, null).color); // 了解
  18. </script>
  19. </body>
  1. (1)获取属性 - demo
  2. (2)获取样式 - demo

3. 获取 dom 节点的内容

  1. <body>
  2. <div id="app">
  3. <div>
  4. aaaaaa
  5. <p>
  6. bbbbbb
  7. </p>
  8. </div>
  9. </div>
  10. <script>
  11. var $app = document.querySelector('#app');
  12. // 获取标签和文本
  13. console.log('innerHTML=', $app.innerHTML);
  14. // 只获取文本
  15. console.log('innerText=', $app.innerText);
  16. </script>
  17. </body>
  1. innerHTML 获取内容连同元素节点
  2. innerText 只获取文本内容

2.2 添加节点

1. 创建节点和插入节点

  1. <body>
  2. <ul id="list">
  3. <li>1</li>
  4. <li>2</li>
  5. <li>3</li>
  6. </ul>
  7. <script>
  8. // 创建li节点
  9. var $li = document.createElement('li');
  10. $li.innerText = 4;
  11. // 把li节点插入到ul里面
  12. var $list = document.querySelector('#list');
  13. $list.appendChild($li);
  14. </script>
  15. </body>

2. 通过点击事件插入节点

  1. <body>
  2. <button onclick="add();">添加节点</button>
  3. <ul id="list">
  4. <li>1</li>
  5. <li>2</li>
  6. <li>3</li>
  7. </ul>
  8. <script>
  9. var num = 4;
  10. function add() {
  11. // 创建li节点
  12. var $li = document.createElement('li');
  13. $li.innerText = num++;
  14. console.log(num);
  15. // 把li节点插入到ul里面
  16. var $list = document.querySelector('#list');
  17. $list.appendChild($li);
  18. }
  19. </script>
  20. </body>

2.3 删除节点

remove()

  1. <body>
  2. <ul id="list">
  3. <li>1</li>
  4. <li>2</li>
  5. <li>3</li>
  6. </ul>
  7. <button onclick="remove();">删除</button>
  8. <script>
  9. function remove() {
  10. var $list = document.querySelector('#list');
  11. $list.remove();
  12. }
  13. </script>
  14. </body>

2.4 修改节点(内容,样式,属性)

  1. - 修改内容 节点.innerHTML=xxx; innerText=xxx;
  2. - 修改样式 节点.style.xxx=xxx; // style是行内样式
  3. - 修改属性 节点.属性名=xxx;
  • 修改内容 节点.innerHTML=xxx; innerText=xxx; ```javascript

  1. <script>
  2. function showDiv() {
  3. var $box = document.querySelector('.box');
  4. var $button = document.querySelector('button');
  5. var flag = $box.style.display;
  6. if (flag === 'none') {
  7. $box.style.display = 'block';
  8. $button.innerText = '隐藏';
  9. } else {
  10. $box.style.display = 'none';
  11. $button.innerText = '显示';
  12. }
  13. }
  14. </script>
  1. - 修改样式 节点.style.xxx=xxx; // style是行内样式
  2. ```javascript
  3. <style>
  4. div {
  5. border: 1px solid;
  6. margin-top: 20px;
  7. }
  8. .dsn {
  9. display: none;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <button onclick="showDiv();">显示</button>
  15. <div class="box">
  16. <p>xxxxxx</p>
  17. <p>xxxxxx</p>
  18. <p>xxxxxx</p>
  19. </div>
  20. <script>
  21. function showDiv() {
  22. var $box = document.querySelector('.box');
  23. var $button = document.querySelector('button');
  24. var className = $box.className;
  25. if (className === 'box') {
  26. $box.className = 'box dsn';
  27. $button.innerText = '隐藏';
  28. } else {
  29. $box.className = 'box';
  30. $button.innerText = '显示';
  31. }
  32. }
  33. </script>
  • 修改属性 节点.属性名=xxx; ```javascript
  1. <button onclick="changeImg(1)">换图1</button>
  2. <button onclick="changeImg(2)">换图2</button>
  3. <div class="box">
  4. <img src="./img/fj1.jpg" alt="">
  5. </div>
  6. <script>
  7. function changeImg(num) {
  8. // 修改img的src属性
  9. document.querySelector('img').src = `./img/fj${num}.jpg`;
  10. }
  11. </script>

  1. <a name="MsSUb"></a>
  2. ## (三)事件绑定
  3. <a name="Ah1gk"></a>
  4. ### 3.1 普通事件绑定(DOM 一级)
  5. - onxxx
  6. - xxx.click
  7. ```javascript
  8. <body>
  9. <button onclick="alert('哈哈哈哈');">点我</button>
  10. <button id="btn">点我2</button>
  11. <script>
  12. var $btn = document.querySelector('#btn');
  13. $btn.onclick = function() {
  14. alert('嘿嘿嘿');
  15. }
  16. </script>
  17. </body>

3.2 高级事件绑定(DOM 二级) addEventListener

dom一级事件绑定和dom二级事件绑定区别: 同类型事件, 前者绑定多次, 只会执行一次, 后者可以执行多次

  1. <body>
  2. <button id="btn1" onclick="alert('哈哈哈哈');">点我</button>
  3. <button id="btn2">点我2</button>
  4. <script>
  5. var $btn = document.querySelector('#btn1');
  6. $btn.onclick = function() {
  7. alert('再绑定一次');
  8. }
  9. var $btn2 = document.querySelector('#btn2');
  10. // 参数1:事件名称 参数2:监听器(监听函数) 参数3:事件模式
  11. $btn2.addEventListener('click', function() {
  12. alert('dom二级事件绑定')
  13. }, false);
  14. // 函数也可以在外边声明
  15. $btn2.addEventListener('click', test, false);
  16. function test() {
  17. alert('再绑定一次')
  18. }
  19. </script>
  20. </body>

3.3 使用 js 触发事件

  1. <body>
  2. <button id="btn1" onclick="console.log('哈哈哈哈');">点我</button>
  3. <button id="btn2">点我2</button>
  4. <script>
  5. var $btn = document.querySelector('#btn1');
  6. $btn.onclick = function() {
  7. console.log('再绑定一次');
  8. }
  9. var $btn2 = document.querySelector('#btn2');
  10. // 参数1:事件名称 参数2:监听器(监听函数) 参数3:事件模式
  11. $btn2.addEventListener('click', function() {
  12. console.log('dom二级事件绑定')
  13. }, false);
  14. // 函数也可以在外边声明
  15. $btn2.addEventListener('click', test, false);
  16. function test() {
  17. console.log('再绑定一次')
  18. }
  19. // 通过js触发事件
  20. $btn.click();
  21. setTimeout(function() {
  22. $btn2.click();
  23. }, 3000)
  24. </script>
  25. </body>

(四) 事件常见类型

1.如图

onchange HTML 元素改变
onclick 用户点击 HTML 元素
onmouseover 用户在一个 HTML 元素上移动鼠标
onmouseout 用户从一个 HTML 元素上移开鼠标
onkeydown 用户按下键盘按键
onkeyup 用户松开键盘按键
onload 浏览器已完成页面的加载
oninput 键盘输入事件
touchStart 触摸开始
touchMove 滑动
  1. <script>
  2. window.onload = function() {
  3. console.log('页面加载完成');
  4. var $button = document.querySelector('button');
  5. $button.onclick = function() {
  6. alert('哈哈哈哈');
  7. }
  8. }
  9. </script>
  10. </head>
  11. <body>
  12. <button>点我</button>
  13. </body>
  1. <body>
  2. <input type="text" name="" id="" oninput="test();" onkeydown="test2();">
  3. <script>
  4. function test() {
  5. var $input = document.querySelector('input');
  6. console.log($input.value);
  7. }
  8. var count = 0;
  9. function test2() {
  10. console.log(`键盘被按下了${++count}次`);
  11. }
  12. </script>
  13. </body>
  1. <body>
  2. <p>xxxx</p>
  3. <p>xxxx</p>
  4. <script>
  5. // 滚动事件
  6. var count = 0;
  7. window.onscroll = function() {
  8. console.log(`滚动了${++count}次`);
  9. }
  10. </script>
  11. </body>

| touchEnd | 触摸结束 |

更多事件类型

  1. https://www.runoob.com/jsref/dom-obj-event.html

(五) 事件对象

1. event 对象: 每个事件被触发时,都会产生一个event对象

  1. <body>
  2. <button onclick="clickMe();">点击我</button>
  3. <input type="text" oninput="inputData()">
  4. <script>
  5. function clickMe() {
  6. console.log(event);
  7. }
  8. function inputData() {
  9. console.log(event);
  10. }
  11. </script>
  12. </body>

2. target 和 currentTarget

  • target表示被触发的元素(比如: 点击事件就是被点击的元素)
  • currentTarget是事件绑定的元素 ```javascript

  1. <script>
  2. function clickMe() {
  3. var target = event.target; // 点击谁就是谁
  4. var currentTarget = event.currentTarget; // div
  5. console.log('被点击的元素是:', target);
  6. console.log('事件绑定的元素:', currentTarget);
  7. }
  8. </script>

  1. <a name="KZoqo"></a>
  2. ## (六) 事件冒泡和事件捕获
  3. <a name="WUUVt"></a>
  4. ### 1. 事件流(事件的传播)
  5. 1. 事件捕获阶段
  6. 1. 目标阶段
  7. 1. 事件冒泡阶段
  8. ```javascript
  9. <style>
  10. #box {
  11. width: 500px;
  12. height: 500px;
  13. background-color: gray;
  14. }
  15. #pp {
  16. width: 300px;
  17. height: 300px;
  18. background-color: green;
  19. padding: 50px;
  20. }
  21. #btn {
  22. position: fixed;
  23. bottom: 20px;
  24. left: 20px;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="box">
  30. <p id="pp">
  31. <button id="btn">事件源</button>
  32. </p>
  33. </div>
  34. <script>
  35. var $box = document.querySelector('#box');
  36. var $pp = document.querySelector('#pp');
  37. var $btn = document.querySelector('#btn');
  38. // false表示在冒泡阶段触发, true表示在捕获阶段触发
  39. $box.addEventListener('click', function() {
  40. console.log('div监听到了事件');
  41. }, false);
  42. $pp.addEventListener('click', function() {
  43. console.log('p标签监听到了事件');
  44. }, false);
  45. $btn.addEventListener('click', function() {
  46. console.log('button标签监听到了事件');
  47. }, false);
  48. </script>

2. 事件冒泡应用: 事件委托(托管)

  1. <style>
  2. li {
  3. border: 1px solid;
  4. margin-top: 10px;
  5. cursor: pointer;
  6. width: 100px;
  7. }
  8. .on {
  9. color: red;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <ul id="app" onclick="clickLi();">
  15. <li>1</li>
  16. <li>2</li>
  17. <li>3</li>
  18. <li>4</li>
  19. <li>5</li>
  20. </ul>
  21. <script>
  22. function clickLi() {
  23. // console.log(event.target);
  24. var target = event.target;
  25. console.log(target);
  26. if (target.nodeName !== 'LI') return false;
  27. // 其他的li清除颜色
  28. var $li = document.querySelector('.on');
  29. if ($li) {
  30. $li.className = ''
  31. }
  32. // 被点击的li改变颜色
  33. target.className = 'on';
  34. }
  35. </script>
  1. 改造计算器

Bom 对象

1. location.href 获取浏览器地址 (window.location)

2. location.href = ‘http://www.baidu.com‘ // 跳转到百度

3. location.href = ‘http://www.baidu.com?username=huruqing
// 跳转到百度的时候,加上参数 username=huruqing

  1. // 1.location对象
  2. // (1) 获取url地址
  3. var urlStr = location.href;
  4. console.log('urlStr:', urlStr);
  5. // (2) 用location.href进行页面跳转
  6. location.href = 'http://www.baidu.com';
  7. // (2) 页面参数传递,比如a页面要传数据给b页面
  8. location.href = 'http://www.baidu.com?a=222&b=333';
  9. // (3) location.search 获取浏览器地址?开始的参数
  10. // 作业: 把字符 ?a=123&b=456,使用对象存起来,比如: {a:123,b:456}
  11. // var str = '?a=123&b=456';
  12. // var obj = {};
  13. var params = location.search;

4. setTimeout 延迟

  1. // (4) 延迟执行
  2. setTimeout(function() {
  3. console.log('一秒后执行')
  4. }, 1000);

5. setInterval 定时器和清除定时器

  1. // (5) setInterval 定时器
  2. var count = 0;
  3. var timer = setInterval(function() {
  4. console.log(count++);
  5. // 当count等于10的时候,清除定时器
  6. if (count === 10) {
  7. // 清除定时器
  8. clearInterval(timer);
  9. }
  10. }, 1000);

6. navigator: userAgent 用来判断用户在使用什么设备

  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. <h3>正在判断用户设备......</h3>
  11. <script>
  12. // 判断用户使用设备
  13. //js判断是否移动端
  14. function fIsMobile() {
  15. return /Android|iPhone|iPad|iPod|BlackBerry|webOS|Windows Phone|SymbianOS|IEMobile|Opera Mini/i.test(navigator.userAgent);
  16. }
  17. setTimeout(function() {
  18. var res = fIsMobile();
  19. if (res) {
  20. location.href = './mobile.html';
  21. } else {
  22. location.href = './pc.html';
  23. }
  24. }, 3000);
  25. </script>
  26. </body>
  27. </html>

7. history对象 浏览器历史对象

  1. history.back();
  2. history.forward();
  3. history.go();