Blogger Information
Blog 33
fans 0
comment 0
visits 18742
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
访问器属性原理和应用场景及dom元素的两个重要方法
李玉峰
Original
337 people have browsed it

访问器属性

用访问器属性改造后:调用和设置方法更简单

访问器属性,本质上还是方法,调用时,用的是属性访问的语法

  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. let users = {
  12. data: {
  13. id: 1,
  14. username: "游戏人生",
  15. salary: "9999",
  16. },
  17. // 获取工资
  18. getSalary() {
  19. return this.data.salary;
  20. },
  21. // 设置工资
  22. setSalary(salary) {
  23. if (salary > 100 && salary < 1000000) {
  24. this.data.salary = salary;
  25. } else {
  26. console.log("工资收入有误");
  27. }
  28. },
  29. };
  30. // 打印查看
  31. console.log(users.getSalary());
  32. users.setSalary(6000);
  33. console.log(users.getSalary());
  34. // 上面收入的获取方法:console.log(users.data.salary)
  35. // 访问器属性改造示例
  36. // 改造后的获取方法:console.log(users.salary)
  37. // 用访问器属性改造后:调用和设置更简单
  38. // 访问器属性,本质上还是方法,调用时,用的是属性访问的语法
  39. users = {
  40. data: {
  41. id: 1,
  42. username: "游戏人生",
  43. salary: "9999",
  44. },
  45. // 获取username
  46. get username() {
  47. return this.data.username;
  48. },
  49. // 获取工资
  50. get salary() {
  51. return this.data.salary;
  52. },
  53. // 设置工资
  54. set salary(salary) {
  55. if (salary > 100 && salary < 1000000) {
  56. this.data.salary = salary;
  57. } else {
  58. console.log("工资收入有误");
  59. }
  60. },
  61. };
  62. console.log("---------------");
  63. console.log(users.username);
  64. users.salary = "6000";
  65. console.log(users.salary);
  66. </script>
  67. </body>
  68. </html>

应用场景:

1、修改访问器属性动态修改相关联的数据属性。

2、监听访问器属性的修改去操作其它业务逻辑。

dom元素的二个重要方法

1.获取一组元素用选择器:querySelectorAll(selector)

2.获取一个元素用选择器:querySelector

  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. </ul>
  17. <script>
  18. // 1.获取一组元素用选择器querySelectorAll(selector)
  19. // 将所有的li.item变成红色
  20. // console.log(document);
  21. console.dir(document);
  22. // querySelectorAll(selector):选择器,功能是返回一组元素
  23. // console.dir(document.querySelectorAll);
  24. // 拿到一组item
  25. console.dir(document.querySelectorAll(".item"));
  26. const items = document.querySelectorAll(".item");
  27. for (let i = 0; i < items.length; i++) {
  28. // 遍历每一行的item
  29. console.log(items[i]);
  30. // 设置前景色为红色
  31. items[i].style.color = "red";
  32. }
  33. // 用forEach更容易,改为蓝色
  34. items.forEach((item) => (item.style.color = "blue"));
  35. console.log("----------------");
  36. // 2.获取一个元素用选择器:querySelector
  37. // querySelector返回的是一组元素中的第一个
  38. // 将第二个改为红色
  39. const firstItem = document.querySelector(".item");
  40. console.log(firstItem);
  41. firstItem.style.color = "green";
  42. // 获取页面中的唯一元素的方法很多。示例
  43. console.log(document.querySelector("body"));
  44. console.log(document.body);
  45. console.log(document.head);
  46. console.log(document.title);
  47. // 获取html,专门用documentElement
  48. console.log(document.documentElement);
  49. </script>
  50. </body>
  51. </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