前端 --- JavaScript WebAPI

x33g5p2x  于2022-04-19 转载在 Java  
字(12.2k)|赞(0)|评价(0)|浏览(516)

1. WebAPI 背景知识

1.1 什么是 WebAPI

JS 分成三个大的部分:

  • ECMAScript: 基础语法部分
  • DOM API: 操作页面结构
  • BOM API: 操作浏览器

WebAPI 就包含了 DOM + BOM.

1.2 DOM 基本概念

什么是 DOM

DOM 全称为 Document Object Model.

W3C 标准给我们提供了一系列的函数, 让我们可以操作:

  1. 网页内容
  2. 网页结构
  3. 网页样式

DOM 树

一个页面的结构是一个树形结构, 称为 DOM 树.

重要概念:

  • 文档: 一个页面就是一个 文档, 使用 document 表示.
  • 元素: 页面中所有的标签都称为 元素. 使用 element表示.
  • 节点: 网页中所有的内容都可以称为 节点(标签节点, 注释节点, 文本节点, 属性节点等). 使用 node表示.

2. 获取元素

2.1 querySelector

使用 querySelector能够完全复用前面学过的 CSS 选择器知识, 达到更快捷更精准的方式获取到元素对象

语法格式:

  1. let element = document.querySelector(selectirs);
  • selectors 填一个或者多个选择器

使用示例:

  1. <div class="one"> abc </div>
  2. <div id="two"> def </div>
  3. <div><input type="text"></div>
  4. <script>
  5. let one = document.querySelector('.one');
  6. console.log(one);
  7. let two = document.querySelector('#two');
  8. console.log(two);
  9. let three = document.querySelector('input');
  10. console.log(three);
  11. </script>

运行截图

2.2 querySelectorAll

如果您需要与指定选择器匹配的所有元素的列表,则应该使用 querySelectorAll()

使用示例:

  1. <div class="one">123</div>
  2. <div id="two">456</div>
  3. <script>
  4. let divs = document.querySelectorAll('div');
  5. console.log(divs);
  6. </script>

运行截图

3. 操作元素

3.1 获取/修改元素内容

1. innerText

Element.innerText属性表示一个节点及其后代的“渲染”文本内容

**注:**不识别 html 标签. 是非标准的(IE发起的). 读取结果不保留html源码中的 换行和空格.

使用示例:

  1. <div class="one">hello world</div>
  2. <div class="two">hello world</div>
  3. <script>
  4. let div = document.querySelector('.two');
  5. console.log(div);
  6. div.innerText = '<span>world hello</span>';
  7. </script>

运行截图:

通过 innerText无法获取div 内部的 html 结构, 只能得到文本内容.

2. innerHTML

Element.innerHTML 属性设置或获取HTML语法表示的元素的后代
注意:

  • 识别 html 标签. W3C 标准的. 读取结果保留html源码中的 换行 和 空格

代码示例:

  1. <div class="one">hello world</div>
  2. <div class="two">hello world</div>
  3. <script>
  4. let div = document.querySelector('.two');
  5. console.log(div);
  6. div.innerHTML = '<span>world hello</span>';
  7. </script>

运行截图:

innerHTML 不光能获取到页面的 html结构, 同时也能修改结构. 并且获取到的内容保留的空格和换行

3.2 获取/修改元素属性

注: 通过 element.属性来获取属性
代码示例:

  1. <img src="male.png" title="男" alt="男">
  2. <script>
  3. let img = document.querySelector('img');
  4. img.onclick = function() {
  5. if(img.title.lastIndexOf("男") != -1){
  6. img.src = 'female.png';
  7. img.title = '女';
  8. }else{
  9. img.src = 'male.png';
  10. img.title = '男';
  11. }
  12. }
  13. </script>

运行结果:

3.3 获取/修改表单元素属性

代码示例1: 播放 暂停的转换.

  1. <input type="button" value="播放">
  2. <script>
  3. let input = document.querySelector('input');
  4. input.onclick = function() {
  5. if(input.value == '播放'){
  6. input.value = '暂停';
  7. }else{
  8. input.value = '播放';
  9. }
  10. }
  11. </script>

运行截图:

代码示例2: 计数

  1. <input type="text" value="0" id="one">
  2. <input type="button" value="点击+1" id="add">
  3. <script>
  4. let one = document.querySelector('#one');
  5. let add = document.querySelector('#add');
  6. add.onclick = function() {
  7. one.value++;
  8. }
  9. </script>

代码示例3: 全选/取消全选按钮

  1. <h3>选择你喜欢玩的游戏</h3>
  2. <input type="checkbox" class="game">王者荣耀<br/>
  3. <input type="checkbox" class="game">和平精英<br/>
  4. <input type="checkbox" class="game">开心消消乐<br/>
  5. <input type="checkbox" class="game">我的世界<br/>
  6. <input type="checkbox" class="all">全选
  7. <script>
  8. let games = document.querySelectorAll('.game');
  9. let all = document.querySelector('.all');
  10. all.onclick = function(){
  11. for (let i = 0; i < games.length; i++) {
  12. games[i].checked = all.checked;
  13. }
  14. }
  15. for (let i = 0; i < games.length; i++) {
  16. games[i].onclick = function() {
  17. all.checked = allChecked();
  18. }
  19. }
  20. function allChecked() {
  21. for (let i = 0; i < games.length; i++) {
  22. if(!games[i].checked){
  23. return false;
  24. }
  25. }
  26. return true;
  27. }
  28. </script>

运行截图

3.4 获取/修改样式属性

CSS 中指定给元素的属性, 都可以通过 JS 来修改
style 中的属性都是使用 驼峰命名 的方式和 CSS 属性对应的.
例如: font-size => fontSize, background-color => backgroundColor

1. 行内样式操作

  1. element.style.[属性名] = [属性值];
  2. element.style.cssText = [属性名+属性值];
代码示例: 字体变大
  1. <div style="font-size: 20px;color:red">你好</div>
  2. <script>
  3. let div = document.querySelector('div');
  4. div.onclick = function() {
  5. let fontSize = parseInt(div.style.fontSize);
  6. fontSize += 10;
  7. div.style.fontSize = fontSize + "px";//注意有单位的要带上单位
  8. }
  9. </script>

运行截图:

2. 类名样式操作

  1. element.className = [CSS 类名];
代码示例: 背景颜色变化
  1. <style>
  2. html,body{
  3. height: 100%;
  4. width: 100%;
  5. }
  6. div {
  7. height: 100%;
  8. width: 100%;
  9. }
  10. .black{
  11. background-color:black;
  12. color: gray;
  13. }
  14. .gray {
  15. background-color: gray;
  16. color: black;
  17. }
  18. </style>
  19. <div class='black'>
  20. 你好!
  21. </div>
  22. <script>
  23. let div = document.querySelector('div');
  24. div.onclick = function() {
  25. if(div.className.indexOf("black") != -1){
  26. div.className = 'gray';
  27. }else{
  28. div.className = 'black';
  29. }
  30. }
  31. </script>

运行截图:

4. 操作节点

4.1 新增节点

分为两个步骤:

  1. 创建元素节点
    createElement创建元素节点.
    createTextNode 创建文本节点
    createComment 创建注释节点
    createAttribute 创建属性节点
  2. 插入节点到 dom 树中
    ① 使用 appendChild 将节点插入到指定节点的最后一个孩子之后
    ②使用insertBefore将节点插入到指定节点之前

代码示例:

  1. <div class="test">
  2. </div>
  3. <script>
  4. let div = document.createElement('div');
  5. div.id = 'mydiv';
  6. div.className = 'one';
  7. div.innerHTML = 'hehe';
  8. let test = document.querySelector('.test');
  9. test.appendChild(div);
  10. </script>

运行截图:

代码示例: 当一个节点插入两次,相当于移动.

  1. <div class="parent">
  2. <div>1</div>
  3. <div>2</div>
  4. <div>3</div>
  5. <div>4</div>
  6. </div>
  7. <script>
  8. let child = document.createElement('div');
  9. child.innerHTML = '100';
  10. let parent = document.querySelector('.parent');
  11. // 本来有四个元素,0号元素没有,就插入一个元素
  12. parent.insertBefore(child,parent.children[0]);
  13. // 插入到2号元素前,1号元素是1不是child,2号元素是2.
  14. parent.insertBefore(child,parent.children[2]);
  15. </script>

运行结果:

4.2 删除节点

使用 removeChild 删除子节点

  1. oldChild = element.removeChild(child);

注: 如果 child 不是 element 的子节点,会抛异常

代码示例:

  1. <div class="parent">
  2. <div class="child">1</div>
  3. <div class="child">2</div>
  4. <div class="child">3</div>
  5. </div>
  6. <script>
  7. let parent = document.querySelector('.parent');
  8. let childs = document.querySelectorAll('.child');
  9. parent.removeChild(childs[1]);
  10. </script>

运行结果:

5. 实现几个案例

5.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>猜数字</title>
  8. </head>
  9. <body>
  10. <div class="parent">
  11. <div><input type="button" value="重新开始一局新游戏" class="again"></div>
  12. <div>
  13. 请输入要猜的数字: <input type="text" class="guessNum"> <input type="button" value="猜" class="press">
  14. </div>
  15. <div>
  16. 已经猜的次数: <span class="count">0</span>
  17. </div>
  18. <div>
  19. 结果: <span class="result"></span>
  20. </div>
  21. </div>
  22. <script>
  23. let guessNum = document.querySelector('.guessNum');
  24. let press = document.querySelector('.press');
  25. let count = document.querySelector('.count');
  26. let result = document.querySelector('.result');
  27. let countCount = 0;
  28. let guessResult = Math.floor(Math.random()*100)+1;
  29. press.onclick = function(){
  30. countCount++;
  31. count.innerHTML = countCount;
  32. guessNumber = parseInt(guessNum.value);
  33. if(guessNumber == guessResult){
  34. result.innerHTML = '猜对了';
  35. result.style = 'color : red';
  36. }else if(guessNumber < guessResult){
  37. result.innerHTML = '猜小了';
  38. result.style = 'color : blue';
  39. }else{
  40. result.innerHTML = '猜大了';
  41. result.style = 'color : orange';
  42. }
  43. }
  44. let again = document.querySelector('.again');
  45. again.onclick = function() {
  46. guessResult = Math.floor(Math.random()*100)+1;
  47. countCount = 0;
  48. count.innerHTML = 0;
  49. guessNum.value = '';
  50. result.innerHTML ='';
  51. }
  52. </script>
  53. </body>
  54. </html>

运行截图:

5.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>表白墙</title>
  8. </head>
  9. <body>
  10. <div class="parent">
  11. <div id="wall">表白墙</div>
  12. <div id="remind">输入后点击提交,会将信息显示在表格中</div>
  13. <div class="one"><span class="two">谁:</span><input type="text" class="text"></div>
  14. <div class="one"><span class="two">对谁:</span><input type="text" class="text"></div>
  15. <div class="one"><span class="two">说什么:</span><input type="text" class="text"></div>
  16. <div class="one"><input type="button" value="提 交" class="press"></div>
  17. </div>
  18. <style>
  19. /* 去除浏览器默认样式 */
  20. * {
  21. margin: 0;
  22. padding: 0;
  23. }
  24. /* 设置总宽度 */
  25. .parent {
  26. width: 400px;
  27. margin: 0 auto;
  28. }
  29. /* 涉资表白墙样式 */
  30. #wall {
  31. font-size: 30px;
  32. font-weight: 700;
  33. text-align: center;
  34. margin: 5px;
  35. }
  36. /* 设置提示信息样式 */
  37. #remind{
  38. font-size:13px;
  39. text-align: center;
  40. color:gray;
  41. margin: 5px;
  42. }
  43. /* 设置弹性布局 */
  44. .one {
  45. display: flex;
  46. justify-content: center;
  47. align-items: center;
  48. height: 40px;
  49. }
  50. /* 设置文字内容 */
  51. .two {
  52. width: 100px;
  53. line-height: 40px;
  54. }
  55. /* 设置输入框 */
  56. .one .text{
  57. width: 200px;
  58. height: 20px;
  59. }
  60. /* 提交按钮的设置 */
  61. .one .press{
  62. width: 304px;
  63. height: 40px;
  64. color:white;
  65. background-color: orange;
  66. border-radius: 5px;
  67. border: none;
  68. }
  69. /* 设置鼠标点击的时候改变颜色 */
  70. .one .press:active{
  71. background-color: red;
  72. }
  73. /* 提交之和内容的设置 */
  74. .elem {
  75. text-align: center;
  76. }
  77. </style>
  78. <script>
  79. // 获取到输入框元素
  80. let texts = document.querySelectorAll('.text');
  81. // 获取到提交按钮元素
  82. let press = document.querySelector('.press');
  83. // 设置单击事件
  84. press.onclick = function() {
  85. let user1 = texts[0].value;
  86. let user2 = texts[1].value;
  87. let message = texts[2].value;
  88. // 如果有一个为空,就提交不成功
  89. if(user1=='' || user2=='' || message==''){
  90. return;
  91. }
  92. // 这里都不为空,创建新的节点
  93. let elem = document.createElement('div');
  94. elem.className = 'elem';
  95. elem.innerHTML = user1 + '对' + user2 + '说: ' +message;
  96. // 插入新的节点
  97. let parent = document.querySelector('.parent');
  98. parent.appendChild(elem);
  99. // 提交之后,将输入框置空.
  100. for(let i = 0; i < 3; i++){
  101. texts[i].value='';
  102. }
  103. }
  104. </script>
  105. </body>
  106. </html>

运行截图:

5.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>待办事项</title>
  8. </head>
  9. <body>
  10. <div class="parent">
  11. <div class="one">
  12. <input type="text" class="text"><input type="button" value="新建任务" class="submit">
  13. </div>
  14. <div class="two">
  15. <div class="left">
  16. <h3>未完成</h3>
  17. </div>
  18. <div class="right">
  19. <h3>已完成</h3>
  20. </div>
  21. </div>
  22. </div>
  23. <style>
  24. /* 去除浏览器默认样式 */
  25. * {
  26. margin: 0;
  27. padding: 0;
  28. }
  29. /* 设置宽度 */
  30. .parent {
  31. width: 840px;
  32. margin: 0 auto;
  33. }
  34. /* 设置输入框和新建的样式 */
  35. .one {
  36. height: 50px;
  37. padding: 20px;
  38. }
  39. /* 设置输入框样式 */
  40. .one .text{
  41. height: 50px;
  42. width: 600px;
  43. }
  44. /* 设置提交框样式 */
  45. .one .submit {
  46. background-color: orange;
  47. color: white;
  48. height: 50px;
  49. width: 196px;
  50. border: none;
  51. }
  52. /* 设置点击时的背景 */
  53. .one .submit:active{
  54. background-color: red;
  55. }
  56. /* 设置已完成和未完成的样式 */
  57. .two{
  58. width: 800px;
  59. height: 800px;
  60. display: flex;
  61. margin: 0 auto;
  62. }
  63. /* 设置未完成和已完成字体样式 */
  64. .two h3 {
  65. height: 50px;
  66. text-align: center;
  67. line-height: 50px;
  68. background-color: black;
  69. color: white;
  70. }
  71. /* 设置未完成左边的样式 */
  72. .left {
  73. width: 50%;
  74. height: 100%;
  75. }
  76. /* 设置已完成右边的样式 */
  77. .right {
  78. width: 50%;
  79. height: 100%;
  80. }
  81. /* 新建任务的样式 */
  82. .row {
  83. height: 50px;
  84. display: flex;
  85. align-items: center;
  86. }
  87. /* 新建任务字体的样式 */
  88. .row span {
  89. width: 340px;
  90. }
  91. /* 新建任务的删除按钮样式 */
  92. .row button{
  93. width: 40px;
  94. height: 40px;
  95. }
  96. </style>
  97. <script>
  98. // 首先获取新建按钮元素
  99. let submit = document.querySelector('.submit');
  100. // 设置鼠标单击事件
  101. submit.onclick = function() {
  102. // 获取输入框元素
  103. let text = document.querySelector('.text');
  104. // 判断输入框内容是否为空
  105. if(text.value == '') return;
  106. // 新建代办事项
  107. let row = document.createElement('div');
  108. row.className='row';
  109. let checkBox = document.createElement('input');
  110. checkBox.type='checkbox';
  111. let thing = document.createElement('span');
  112. thing.innerHTML = text.value;
  113. let del = document.createElement('button');
  114. del.innerHTML='删除';
  115. row.appendChild(checkBox);
  116. row.appendChild(thing);
  117. row.appendChild(del);
  118. // 获取左边元素
  119. let left = document.querySelector('.left');
  120. left.appendChild(row);
  121. // 添加节点之后置空
  122. text.value='';
  123. // 设置选择框的鼠标单击事件
  124. checkBox.onclick = function() {
  125. // 如果被选择了就移动已完成
  126. // 如果未完成就移动到未完成
  127. if(checkBox.checked){
  128. let target = document.querySelector('.right');
  129. target.appendChild(row);
  130. }else{
  131. let target = document.q
  132. uerySelector('.left');
  133. target.appendChild(row);
  134. }
  135. }
  136. // 设置删除按钮的鼠标单击事件
  137. del.onclick = function() {
  138. // 使用 parentNode 获取到父节点
  139. let parent = row.parentNode;
  140. // 删除该节点
  141. parent.removeChild(row);
  142. }
  143. }
  144. </script>
  145. </body>
  146. </html>

运行截图:

相关文章