Blogger Information
Blog 11
fans 0
comment 0
visits 6373
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js-基础(三)访问器原理与应用场景、获取dom元素
Technology has temperature
Original
702 people have browsed it

1.访问器原理与应用场景

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

  1. // 设置一个常规的类
  2. let userInfo={
  3. data:{
  4. name:'Jason',
  5. birthday:[2000,12,04],
  6. height:'1.75m',
  7. weight:'70kg'
  8. },
  9. // 获取姓名
  10. // getName(){
  11. // return this.data.name;
  12. // },
  13. // 改写
  14. get Name(){
  15. return this.data.name;
  16. },
  17. // 修改姓名
  18. // ChangeName(name){
  19. // if (name.length <= 20 ){
  20. // this.data.name=name;
  21. // }else{
  22. // console.log('名字过长')
  23. // }
  24. // },
  25. // 改写
  26. set Name(name){
  27. if (name.length <= 20 ){
  28. this.data.name=name;
  29. }else{
  30. console.log('名字过长')
  31. }
  32. },
  33. // 获取年龄
  34. get Age(){
  35. return (2022-this.data.birthday[0])
  36. },
  37. // 获取身高
  38. get Height(){
  39. return `${this.data.height[0]}${this.data.height[2]}${this.data.height[3]} cm`
  40. },
  41. // 修改身高
  42. set Height(height){
  43. return this.data.height=height;
  44. }
  45. }
  46. // 打印属性
  47. console.log(userInfo.data);
  48. console.log(userInfo.Name);
  49. console.log(userInfo.Age);
  50. console.log(userInfo.Height);
  51. userInfo.Height='1.88';
  52. userInfo.Name='Authur';
  53. console.log(userInfo.data);
  54. console.log(userInfo.Name);
  55. console.log(userInfo.Age);
  56. console.log(userInfo.Height);

获取dom元素

  1. <ul class="list">
  2. <li class="item">item1</li>
  3. <li class="item">item2</li>
  4. <li class="item">item3</li>
  5. <li class="item">item4</li>
  6. <li class="item">item5</li>
  7. </ul>
  8. <script>
  9. // 1. 将所有的li.item 变成红色
  10. console.dir(document);
  11. // querySelectorAll(selector):返回一组元素
  12. console.dir(document.querySelectorAll('.item'));
  13. const items = document.querySelectorAll('.item');
  14. for (let i = 0; i < items.length; i++) {
  15. // console.log(items[i]);
  16. items[i].style.color = 'red';
  17. }
  18. // items.forEach(item => (item.style.color = 'green'));
  19. // 2. 将第一个改为蓝色
  20. // querySelector: 返回一组元素中的第一个,只返回一个,而不是一组
  21. const firstItem = document.querySelector('.item');
  22. // console.log(firstItem);
  23. firstItem.style.color = 'blue';
  24. console.log(document.querySelector('body'));
  25. console.log(document.body);
  26. console.log(document.head);
  27. console.log(document.title);
  28. // 获取html
  29. console.log(document.documentElement);
  30. </script>
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