Blogger Information
Blog 21
fans 0
comment 0
visits 9987
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示二个获取元素的api 和dom常用的遍历方法
放手去爱
Original
719 people have browsed it

获取元素的api

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. <form action="login.php" method="post" id="login">
  11. <fieldset class="login" style="display: inline-grid; gap: 10px">
  12. <legend>用户登录</legend>
  13. <div>
  14. <label for="email">邮箱:</label>
  15. <input type="email" name="email" id="email" value="admin@php.cn" autofocus />
  16. </div>
  17. <div>
  18. <label for="password">密码:</label>
  19. <input type="password" name="password" id="password" value="123456" />
  20. </div>
  21. <button>提交</button>
  22. </fieldset>
  23. </form>
  24. <script>
  25. // 1. 获取表单: forms.id
  26. console.log(document.forms.login)
  27. // 2. 获取表单控件: forms.id.name/id
  28. console.log(document.forms.login.email)
  29. // 3. 获取表单控件的值: forms.id.name.value
  30. console.log(document.forms.login.email.value)
  31. // 前后端分离
  32. /// 前端 <->(JSON格式的字符串) <-> 服务器端
  33. // 将表单中的用户邮箱和密码发送到服务器端
  34. // 第一步: 获取表邮箱和密码
  35. let login = document.forms.login
  36. let email = login.email.value
  37. let password = login.password.value
  38. // 第二步: 转为 JS 对象
  39. let user = { email, password }
  40. // 第三步: 把JS对象序列化成JSON字符串
  41. let json = JSON.stringify(user)
  42. console.log(json)
  43. /**
  44. * * 总结
  45. * * 1. 表单: form.id
  46. * * 2. 表单控件: input.name
  47. * * 3. 表单控件的值: input.name.value
  48. */
  49. </script>
  50. </body>
  51. </html>

dom常用的遍历方法

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>item1</li>
  12. <li>item2</li>
  13. <li>item3</li>
  14. <li>item4</li>
  15. <li>item5</li>
  16. <li>item6</li>
  17. </ul>
  18. <script>
  19. /**
  20. * * 节点类型(12个,常用3个)
  21. * * 1. document: 文档类型, 9
  22. * * 2. element: 元素类型, 1
  23. * * 3. text: 文本类型, 3
  24. */
  25. // 列表<ul class='list'>
  26. let list = document.querySelector('.list')
  27. // nodeType 节点类型
  28. console.log(list.nodeType)
  29. // 1. 所有子节点: childNodes
  30. let items = list.childNodes
  31. console.log(items)
  32. // 类数组
  33. // 1. Array.from
  34. console.log(Array.from(items))
  35. // 2. ...rest
  36. console.log([...items])
  37. // 转为数组之后,Array很多API都可以用
  38. // 要求只返回“元素类型的节点”
  39. let result = [...items].filter(item => item.nodeType === 1)
  40. console.log(result)
  41. // 封装
  42. const getChildren = items => [...items].filter(item => item.nodeType === 1)
  43. console.log(getChildren(items))
  44. // js已经给我们封装好一个属性 children
  45. // 2. children : 所有子元素节点
  46. console.log(list.children)
  47. // children : 类数组
  48. // 3. 第一个子元素
  49. // ele.textContent: 元素内的文本
  50. console.log(list.children[0].textContent)
  51. // 语法糖
  52. let first = list.firstElementChild
  53. console.log(first.textContent)
  54. // 4. 后一个
  55. let next = list.querySelector(':first-child + *')
  56. next.style.color = 'red'
  57. next = first.nextElementSibling
  58. // 5. 最后一个
  59. let last = list.lastElementChild
  60. last.style.color = 'red'
  61. // 6. 前一个
  62. let prev = last.previousElementSibling
  63. prev.style.color = 'violet'
  64. // 7. 父节点: 一定是元素或文档类型
  65. let parent = first.parentNode
  66. console.log(parent)
  67. parent.style.border = '1px solid'
  68. // 8. 遍历
  69. // 在 [],{},()前分号不能省
  70. // 为什么不能用forEach,因为返回的不是 NodeList
  71. ;[...list.children].forEach(item => (item.style.border = '1px solid red'))
  72. </script>
  73. </body>
  74. </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