Blogger Information
Blog 13
fans 0
comment 0
visits 6612
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
访问器属性和获取dom元素
shooter
Original
342 people have browsed it

访问器属性

效果图:

代码

  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. <script>
  11. //访问器属性
  12. let user = {
  13. //常规属性
  14. data: {
  15. name: '吴彦祖',
  16. age: 18,
  17. },
  18. //获取年龄
  19. getAge() {
  20. return this.data.age;
  21. },
  22. //设置年龄
  23. setAge(age) {
  24. if (age >= 18 && age <= 120) {
  25. this.data.age = age;
  26. } else {
  27. console.log('非法数据');
  28. }
  29. },
  30. };
  31. console.log(user.getAge());
  32. user.setAge(80);
  33. console.log(user.getAge());
  34. // 对于用户来说,获取年龄,习惯性会用这种方式来获取
  35. // console.log(user.age);
  36. // console.log(user.data.age);
  37. user = {
  38. //常规属性
  39. data: {
  40. name: '彭于晏',
  41. age: 18,
  42. },
  43. //将传统的方法。修改一个伪装成属性的方法
  44. get age() {
  45. return this.data.age;
  46. },
  47. // 设置年龄,将之前的设置方法修改成一个属性
  48. // 方法->属性:伪装成方法的属性,“访问器属性”
  49. set age(age) {
  50. if (age >= 18 && age <= 120) {
  51. this.data.age = age;
  52. } else {
  53. console.log('非法数据');
  54. }
  55. },
  56. };
  57. console.log(user.age);
  58. user.age = 150;
  59. console.log(user.age);
  60. // 访问器属性,本质上还是方法,调用时,用的属性访问的语法
  61. </script>
  62. </body>
  63. </html>

获取DOM元素

效果图

代码

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