Blogger Information
Blog 47
fans 3
comment 0
visits 38232
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
箭头函数、rest,sprend参数、获取dom元素、classlist与dataset对象
Original
1115 people have browsed it

1.箭头函数

箭头函数是用来简化匿名函数的语法糖
=> 胖箭头
-> 小箭头

  1. // => 胖剪头
  2. // -> 小箭头
  3. let id = 'C888';
  4. let name = '电脑';
  5. let model = 'MacBook pro';
  6. let demo7 = (id, name, model) => [id, name, model];
  7. res = demo7(id, name, model);
  8. res.forEach(item => console.log(item));

2.rest,sprend参数使用方法

在函数的参数中…rest是归并
在函数的调用的参数列表中就是…sprend展开

  • rest

    1. function demo4(...arr) {
    2. console.log(arr);
    3. let res = 0;
    4. for (let v of arr) {
    5. res = res + v;
    6. }
    7. console.log(res);
    8. }
    9. demo4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  • sprend

  1. function demo4(...arr) {
  2. console.log(arr);
  3. let res = 0;
  4. for (let v of arr) {
  5. res = res + v;
  6. }
  7. console.log(res);
  8. }
  9. let arr1 = [1, 2, 3, 4, 5, 6, 7, 8];
  10. //spread
  11. demo4(...arr1);

3.获取dom元素方式

  • 获取匹配的元素集合中第一个元素,返回一个

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>获取dom元素</title>
  6. <style>
  7. .item:first-of-type {
  8. color: red;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <ul id="list">
  14. <li class="item">item1</li>
  15. <li class="item">item2</li>
  16. <li class="item">item3</li>
  17. <li class="item">item4</li>
  18. <li class="item">item5</li>
  19. <li class="item">item6</li>
  20. </ul>
  21. <script>
  22. const li = document.querySelector('.item')
  23. console.log(li);
  24. </script>
  • 返回匹配的元素集合所有成员
  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>获取dom元素</title>
  6. <style>
  7. .item:first-of-type {
  8. color: red;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <ul id="list">
  14. <li class="item">item1</li>
  15. <li class="item">item2</li>
  16. <li class="item">item3</li>
  17. <li class="item">item4</li>
  18. <li class="item">item5</li>
  19. <li class="item">item6</li>
  20. </ul>
  21. <script>
  22. const lis = document.querySelectorAll('.item');
  23. // 将数组通过...sprend语法转为真正的数组
  24. let list = [...lis];
  25. console.log(list);
  26. </script>
  27. </body>
  28. </html>

4.classlist对象与dataset对象

  • classList对象

    classlist 专门用来操作类class

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>classlist对象</title>
  6. <style>
  7. .red {
  8. color: red;
  9. }
  10. .blue {
  11. color: blue;
  12. }
  13. .bg {
  14. background-color: yellow;
  15. }
  16. </style>
  17. </head>
  18. <p class="red" id="hello">hello world</p>
  19. <body>
  20. <script>
  21. const p = document.querySelector('p');
  22. // 给p标签添加背景色
  23. p.classList.add('bg');
  24. // 将p标签的颜色字体红色替换为蓝色
  25. p.classList.replace('red', 'blue');
  26. </script>
  27. </body>
  28. </html>
  • dataset对象

    dataset对象专用于访问自定义的标签属性

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>dataset对象</title>
  6. </head>
  7. <body>
  8. <div class="user" data-email='12138@admin.com' data-my-gender="女">个人简介</div>
  9. <script>
  10. const user = document.querySelector('.user');
  11. user.id = '12138';
  12. console.log(user.id);
  13. console.log(user.dataset.email);
  14. console.log(user.dataset.myGender);
  15. </script>
  16. </body>
  17. </html>
Correcting teacher:天蓬老师天蓬老师

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