Blogger Information
Blog 50
fans 0
comment 0
visits 31574
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
获取元素与遍历
手机用户1580651468
Original
298 people have browsed it

获取元素与遍历

一. 实例演示二个获取元素的api

一)获取表单的两个api

1.代码

  1. <script>
  2. // 1. 获取表单: forms.id
  3. console.log(document.forms.login)
  4. // 2. 获取表单控件: forms.id.name/id
  5. console.log(document.forms.login.email)
  6. // 3. 获取表单控件的值: forms.id.name.value
  7. console.log(document.forms.login.email.value)
  8. // 前后端分离
  9. /// 前端 <->(JSON格式的字符串) <-> 服务器端
  10. // 将表单中的用户邮箱和密码发送到服务器端
  11. // 第一步: 获取表邮箱和密码
  12. let login = document.forms.login
  13. let email = login.email.value
  14. let password = login.password.value
  15. // 第二步: 转为 JS 对象
  16. let user = { email, password }
  17. // 第三步: 把JS对象序列化成JSON字符串
  18. let json = JSON.stringify(user)
  19. console.log(json)
  20. /**
  21. * * 总结
  22. * * 1. 表单: form.id
  23. * * 2. 表单控件: input.name
  24. * * 3. 表单控件的值: input.name.value
  25. */
  26. </script>

2.演示的效果

二. 实例演示dom常用的遍历方法

一)代码

  1. ;[...list.children].forEach(item => (item.style.border = '1px solid red'))

二)实现效果

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