Blogger Information
Blog 29
fans 0
comment 0
visits 18507
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
js函数与js操作dom
CC
Original
529 people have browsed it

函数

  • 函数定义
    1. // 定义
    2. function demo1(){
    3. console.log('例子1',demo1.name)
    4. };
    5. // 调用
    6. demo1()
  • 函数传参数
    1. function tc(name){
    2. console.log('i am',name);
    3. }
    4. tc('chen')
  • rest语法,使剩余参数显示
    1. function tc(...shu){
    2. console.log(shu);
    3. }
    4. tc(1,2,3,4,5,6)
  • 遍历累加
    1. function tc(...shu){
    2. console.log(shu);
    3. let sum = 0;
    4. // 遍历数组
    5. for(let v of shu){
    6. sum=sum+v
    7. }
    8. console.log(sum)
    9. }
    10. tc(1,2,3,4,5,6)
  • 函数与dom1(函数作为事件触发不加小括号)
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <button id='btn1'>按钮</button>
  10. </body>
  11. <script>
  12. function fun1(){
  13. alert('hello')
  14. }
  15. // 获取id事件,赋值等于btn1
  16. let btn1=document.getElementById('btn1');
  17. // 点击触发函数,调用函数不加括号
  18. btn1.onclick=fun1
  19. </script>
  20. </html>
  • 函数返回对象
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. function fun1() {
  11. return {
  12. name: 1,
  13. sex: 'nan',
  14. }
  15. }
  16. let preson = fun1();
  17. console.log(preson);
  18. // 使用.访问对象值
  19. console.log(preson.name,res.sex);
  20. </script>
  21. </body>
  22. </html>
  • 函数返回数组
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. function arr1() {
  11. return ["name", "sex"];
  12. }
  13. fun1 = arr1(
  14. )
  15. // 打印数组
  16. console.log(fun1)
  17. // 遍历数组
  18. for (let x in fun1) {
  19. console.log(fun1[x])
  20. }
  21. </script>
  22. </body>
  23. </html>
  • 箭头函数

匿名函数可以改写

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. const fun1 = function (chen) {
  11. return chen * 2
  12. }
  13. console.log(fun1(3))
  14. </script>
  15. </body>
  16. </html>
  17. `
  1. // 去掉function,在()后面加=>
  2. const fun1 = (chen)=> {
  3. return chen * 2
  4. }
  5. console.log(fun1(3))

有返回值,可以去掉return与花括号

  1. // const fun1 = function (chen) {
  2. // return chen * 2
  3. // }
  4. // console.log(fun1(3))
  5. const add = (a,b)=> {
  6. return a+b
  7. }
  8. console.log(add(1,2))
  1. const add = (a,b)=> a+b
  2. console.log(add(1,2))

操作bom

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <style>
  9. .item:first-of-type {
  10. color: red;
  11. }
  12. </style>
  13. <body>
  14. <ul id="list">
  15. <li class="item">item1</li>
  16. <li class="item">item2</li>
  17. <li class="item">item3</li>
  18. <li class="item">item4</li>
  19. <li class="item">item5</li>
  20. <li class="item">item6</li>
  21. </ul>
  22. </body>
  23. <script>
  24. // 返回元素集合中的第一个元素
  25. const li = document.querySelector('.item')
  26. console.log(li)
  27. const lis = document.querySelectorAll('.item')
  28. // 返回元素集合中的所有元素
  29. console.log(lis)
  30. // 转化为数组
  31. let list = [...lis]
  32. console.log(list)
  33. lis.forEach(item => {
  34. console.log(item);
  35. // 所选元素变成蓝色
  36. item.style.color = 'blue';
  37. });
  38. // 选择class
  39. console.log(document.querySelector('.item'))
  40. </script>
  41. </html>

操作dom,获取id,class,操作元素

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <style>
  9. .red {
  10. color: red;
  11. }
  12. .blue{
  13. color: blue;
  14. }
  15. </style>
  16. <body>
  17. <p class="red" id='hello'>大家晚上好</p>
  18. </body>
  19. <script>
  20. const p =document.querySelector('p');
  21. // 获取类名字
  22. console.log(p.className)
  23. // 后去id名字
  24. console.log(p.id)
  25. // 将红色改掉蓝色
  26. p.className='blue'
  27. </script>
  28. </html>

增删改css样式

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <style>
  9. .red {
  10. color: red;
  11. }
  12. .blue{
  13. color: blue;
  14. }
  15. .bg {
  16. background-color: yellow;
  17. }
  18. </style>
  19. <body>
  20. <p >大家晚上好</p>
  21. </body>
  22. <script>
  23. const p =document.querySelector('p');
  24. // 改成蓝色
  25. p.classList.add('blue');
  26. // 增加黄色背景
  27. p.classList.add('bg')
  28. </script>
  29. </html>
  1. const p =document.querySelector('p');
  2. // 改成蓝色,增加黄色背景
  3. p.classList.add('blue','bg');
  4. // 移除黄色背景
  5. p.classList.remove('bg');

自动切换样式

  1. const p =document.querySelector('p');
  2. p.classList.toggle('red');
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
Author's latest blog post