Blogger Information
Blog 46
fans 2
comment 0
visits 19148
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1. 自定义类数组,并说出与纯数组的区别与联系,哪些地方会用到类数组 2. 获取dom元素的API有几个,他们的使用场景是什么? 3. 如何优雅的获取form表单元素与控件的值? 4. dom元素的遍历与常用API有哪些, 实例演示
P粉314265155
Original
312 people have browsed it

浏览器的js属性

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>浏览器的js属性</title>
  8. </head>
  9. <body>
  10. <!-- 1、预定义属性 id \class、style -->
  11. <button id="1" class="2" style="color: red;">按钮1</button>
  12. <!-- 2、z自定义属性 data-xxx-->
  13. <button id="3" class="4" data-text="自定义">自定义按钮</button>
  14. <!-- 3、预定义事件 on+事件名称 事件属性为JS代码 -->
  15. <button onclick="document.body.style.background='blue'">预定义事件按键</button>
  16. <!-- 写到事件属性的js 仅限对当前目标有效 内联脚本、行内脚本 js作用域 -->
  17. <!-- 如果想点击变色,再次点击还原 -->
  18. <button onclick="set(this)">背景变色</button>
  19. <!-- js 代码可以自动合并 -->
  20. <script>
  21. // 开关
  22. let status = false;
  23. function set (a) {
  24. status = !status;
  25. let bg = status?'yellow' :null;
  26. let text =status ?'还原背景' : '设置背景';
  27. document.body.style.backgroundColor =bg;
  28. a.textContent = text;
  29. }
  30. </script>
  31. <button onclick="set(this)">背景变色</button>
  32. <!-- js 外部引入 ,主语js代码要放到后面,不要放到前面 -->
  33. <script src="../0725/js/zuoye.js"></script>
  34. </body>
  35. </html>
  1. <!-- 外部引入js -->
  2. // 开关
  3. let status = false;
  4. function set (a) {
  5. status = !status;
  6. let bg = status?'yellow' :null;
  7. let text =status ?'还原背景' : '设置背景';
  8. document.body.style.backgroundColor =bg;
  9. a.textContent = text;
  10. }
  11. ·
效果

自定义数组

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <script>
  11. const name = ['小红','小黑','小明'];
  12. console.log(name);
  13. console.log(name[0]);
  14. console.log('------------------');
  15. // 类数组 类似数组
  16. const animals = {
  17. 0 : 'dog',
  18. 1:'cat',
  19. 3:'pig',
  20. };
  21. // 访问方式
  22. // 方法一 0是非法标识符 用【】
  23. console.log(animals[0],animals[1],animals[2]);
  24. // 数组特征
  25. // 1、数组就是一个对象,属性从 0开始递增 有 length属性
  26. // 2、类数组跟 数组类似
  27. </script>
  28. </body>
  29. </html>
效果

dom元素获取

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>dom元素获取</title>
  8. </head>
  9. <body>
  10. <ul class="list">
  11. <li class="item">item1</li>
  12. <li class="item">item2</li>
  13. <li class="item">item3</li>
  14. <li class="item">item4</li>
  15. <li class="item">item5</li>
  16. <li class="item">item6</li>
  17. </ul>
  18. <script>
  19. // 获取模型/模式两种
  20. // 1、获取一组 querySelectorAll()
  21. // 2、获取一个querySelector()
  22. // 一、一次性获取一组
  23. // 获取ul list 注意 .lits 跟.item之间要有空格
  24. const items = document.querySelectorAll('.list .item');
  25. // douquerySelectorAll()
  26. console.log(items);
  27. // 获取数组接口 注意要keys 复数 节点列表对象
  28. console.log(items.keys());
  29. // 数组采用 for of遍历 获取 键
  30. for (let k of items.keys() )
  31. console.log(k);
  32. // 数组采用 for of遍历 获取 值
  33. for (let v of items.values() )
  34. console.log(v);
  35. // 数组采用 for of遍历 获取 entries 键值对 数组化
  36. for (let kv of items.entries() )
  37. console.log(kv);
  38. console.log('----------------');
  39. // 简化版 forEach(callback) callback 是指回调函数 两个值 第二值 是可选的
  40. // function (当前正在遍历的值,当前值的索引){ } 注意变量跟forEach位置及括弧
  41. items.forEach(function(v,k){
  42. console.log(k,v);
  43. });
  44. console.log('----------------');
  45. items.forEach(function(v){
  46. console.log(v);
  47. // 修改颜色
  48. v.style.backgroundColor = 'red';
  49. });
  50. console.log('----------------');
  51. // 进一步简化 胖箭头
  52. items.forEach(v => console.log(v));
  53. // 修改颜色
  54. items.forEach(v => v.style.backgroundColor = 'blue');
  55. // 二、一次性获取一个 返回的是数组集合的第一个
  56. // document.querySelectorAll()中括弧里面可以使用伪类获取某一个 类似id
  57. // const eles = document.querySelectorAll('.list .item:first-of-type');
  58. // console.log(eles);
  59. const fi = document.querySelectorAll('.list .item:first-of-type');
  60. // 获取第一个
  61. console.log(fi);
  62. // 获取第一个
  63. console.log(fi[0]);
  64. // 给第一个加背景色
  65. fi[0].style.backgroundColor = 'red';
  66. // 优化简版获取
  67. const first = document.querySelector('.list .item');
  68. console.log(first);
  69. // 给第一个 字体换上白色
  70. first.style.color = 'white';
  71. console.log('----------------');
  72. // querySelector/querySelectorAll: 可以在任何元素上调用,不是只用在document
  73. // 获取一组元素 ul 另一 方法
  74. const one = document.querySelector('.list');
  75. console.log(one);
  76. console.log('----------------');
  77. // const ones = list.querySelectorAll('.item');
  78. // const ones = list.querySelectorAll('.item');
  79. // ones.forEach(li => (li.style.backgroundColor = 'violet'));
  80. // 注意词汇 one 已经在上面定义获取为 .list 了
  81. const ones = one.querySelectorAll('.item');
  82. ones.forEach(li => (li.style.backgroundColor = 'violet'));
  83. // 3、快捷方式
  84. console.log(document.querySelector('body'));
  85. console.log(document.body);
  86. </script>
  87. </body>
  88. </html>
效果

表单元素

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>form表单元素</title>
  8. </head>
  9. <body>
  10. <form aaction="login.php" method="post" id="login">
  11. <!-- fieldset是控件组标签,该标签内容的周围将绘制边框 -->
  12. <fieldset class="login">
  13. <!-- legend 元素为 fieldset 元素定义标题 -->
  14. <legend>用户登录</legend>
  15. <label for="zhanghu">账户:</label>
  16. <input type="zhanghu" name="zhanghu" id="zhanghu" value="18963300001" autofocus>
  17. <br>
  18. <label for="password">密码:</label>
  19. <input type="password" name="password" id="password" value="123456" autofocus>
  20. <br><button>提交</button>
  21. </fieldset>
  22. <script>
  23. // 作业3的方式
  24. const form = document.querySelector('#login');
  25. console.log(form);
  26. console.log(document.querySelector('#zhanghu').value);
  27. console.log('-----------');
  28. // 简洁化 forms. 三种方式
  29. console.log(document.forms[0]);
  30. console.log('-----------');
  31. console.log(document.forms['login']);
  32. console.log('-----------');
  33. console.log(document.forms.login);
  34. console.log('-----------');
  35. // id 相当于name 反过来一样
  36. // 可以跟css 一样 一层一层一点点获取
  37. console.log(document.forms.login .zhanghu.value);
  38. // 前后端分离, 前端发送json到服务器
  39. // json: js对象的序列化,字符串
  40. // 第一步先获取值
  41. let zhanghu = document.forms.login .zhanghu.value;
  42. let password = document.forms.login .password.value;
  43. // 第二 步 开始组装数组
  44. let user = { zhanghu,password};
  45. // 第三步 开始组装js
  46. let json = JSON.stringify(user);
  47. console.log(json);
  48. </script>
  49. </form>
  50. </body>
  51. </html>
效果

dom树

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>遍历dom树</title>
  8. </head>
  9. <body>
  10. <ul class="list">
  11. <li class="item">item1</li>
  12. <li class="item">item2</li>
  13. <li class="item">item3</li>
  14. <li class="item">item4</li>
  15. <li class="item">item5</li>
  16. <li class="item">item6</li>
  17. </ul>
  18. <script>
  19. // 1、节点类型
  20. // 2、Windows 全局对象
  21. // 2、document 文档对象
  22. // 3、element 元素对象
  23. // 4、textcontent 文本对象
  24. let ul = document.querySelector ('.list');
  25. console.log(ul);
  26. console.log(ul.children);
  27. // ul节点对象,包含回车
  28. console.log(ul.childNodes);
  29. ul.childNodes.forEach(node => {
  30. // 如果节点类型 是1 标识约束节点
  31. if (node.nodeType ==1){
  32. console.log(node);
  33. }
  34. });
  35. // 只返回元素类型的节点,我们需要关注。类数组形式
  36. console.log(ul.children);
  37. // 给所有元素变颜色
  38. // 第一步,类数组变为真数组 不建议
  39. console.log(Array.from(ul.children));
  40. // 简化版 类数组变为真数组 ...rest
  41. console.log(...ul.children);
  42. // 手动放到数组内 并使用foreach遍历
  43. [...ul.children].forEach(li =>(li.style.color = 'blue'));
  44. // 遍历
  45. // 获取第一个
  46. console.log([...ul.children][0]);
  47. // 给第一个加背景色
  48. [...ul.children][0].style.backgroundColor = 'red';
  49. // 优雅简答
  50. // 获取第一个
  51. ul.firstElementChild.style.backgroundColor = 'red';
  52. // 获取下一个
  53. ul.firstElementChild.nextElementSibling.style.backgroundColor = 'red';
  54. // 获取最后一个
  55. [...ul.children][ul.children.length - 1].style.backgroundColor = 'red';
  56. // 获取最后一个再次简化
  57. ul.lastElementChild.style.backgroundColor = 'blue';
  58. // 获取最后一个的前一个
  59. ul.lastElementChild.previousElementSibling.style.backgroundColor = 'blue';
  60. // 获取父节点 父节点一定是元素 或文档节点
  61. ul.lastElementChild.parentElement.style.border = '5px solid red'
  62. </script>
  63. </body>
  64. </html>

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!