11_垂直导航.html 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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>Document</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. ul {
  14. list-style: none;
  15. }
  16. h2 {
  17. width: 300px;
  18. height: 50px;
  19. background: black;
  20. color: white;
  21. }
  22. ul{
  23. display: none;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container">
  29. <h2>管理区</h2>
  30. <ul>
  31. <li>111</li>
  32. <li>111</li>
  33. <li>111</li>
  34. </ul>
  35. <h2>交流区</h2>
  36. <ul>
  37. <li>456</li>
  38. <li>456</li>
  39. <li>456</li>
  40. </ul>
  41. </div>
  42. <script>
  43. var h2 = document.getElementsByTagName('h2')
  44. for (var i = 0; i < h2.length; i++) {
  45. h2[i].onclick = function () {
  46. var ul1 = next(this)
  47. console.log(ul1)
  48. if(ul1.style.display == 'block'){
  49. ul1.style.display = 'none'
  50. } else {
  51. ul1.style.display = 'block'
  52. }
  53. }
  54. }
  55. function next(elem){
  56. do{
  57. elem = elem.nextSibling
  58. } while(elem.nodeType != 1)
  59. return elem
  60. }
  61. </script>
  62. </body>
  63. </html>