Blogger Information
Blog 33
fans 0
comment 0
visits 17124
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
访问器属性原理与应用场景,获取 dom 元素的两个重要方法
lucaslwk
Original
330 people have browsed it

访问器属性原理与应用场景,获取 dom 元素的两个重要方法

一.访问器属性原理与应用场景

  1. //常规写法
  2. let teacher = {
  3. data: {
  4. class: "一班",
  5. lesson: "语文",
  6. },
  7. getlesson() {
  8. return this.data.lesson;
  9. },
  10. setlesson(lesson) {
  11. this.data.lesson = lesson;
  12. },
  13. who() {
  14. console.log(`${this.data.class}${this.data.lesson}老师`);
  15. },
  16. };
  17. console.log(teacher.getlesson());
  18. teacher.setlesson("数学");
  19. teacher.who();
  20. //访问器属性,将方法伪装成属性
  21. //调用时使用属性访问语法的方法
  22. teacher = {
  23. data: {
  24. class: "一班",
  25. lesson: "语文",
  26. },
  27. get lesson() {
  28. return this.data.lesson;
  29. },
  30. set lesson(lesson) {
  31. this.data.lesson = lesson;
  32. },
  33. who() {
  34. console.log(`${this.data.class}${this.lesson}老师`);
  35. },
  36. };
  37. console.log(teacher.lesson);
  38. teacher.lesson = "数学";
  39. teacher.who();

二.获取 dom 元素的两个重要方法

  1. //获取class为item的全部元素
  2. const items = document.querySelectorAll(".item");
  3. for (let index = 0; index < items.length; index++) {
  4. items[index].style.color = "orange";
  5. }
  6. //获取class为item的第一个元素
  7. const firstItem = document.querySelector(".item");
  8. firstItem.style.color = "green";
  9. //获取id为item2的元素
  10. const secondItem = document.getElementById("item2");
  11. secondItem.style.color = "blue";
  12. //获取标题,头部,主体
  13. console.log(document.title);
  14. console.log(document.head);
  15. console.log(document.body);
  16. //获取整个html
  17. console.log(document.documentElement);
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