11.垂直导航.html 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. list-style: none;
  12. text-decoration: none;
  13. box-sizing: border-box;
  14. }
  15. ul {
  16. display: none;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <h2>管理区</h2>
  22. <ul id="manager">
  23. <li>1</li>
  24. <li>2</li>
  25. <li>3</li>
  26. <li>4</li>
  27. </ul>
  28. <h2>内容区</h2>
  29. <ul id="main">
  30. <li>a</li>
  31. <li>b</li>
  32. <li>c</li>
  33. <li>d</li>
  34. </ul>
  35. <script>
  36. var h2 = document.getElementsByTagName("h2");
  37. console.log(h2)
  38. for(var i=0; i<h2.length;i++) {
  39. h2[i].onclick = function() {
  40. // console.log(this.nextSibling);
  41. var ul1 = next(this)
  42. console.log(ul1);
  43. // ul1.style.display = 'block'
  44. if(ul1.style.display == 'none') {
  45. ul1.style.display = 'block'
  46. } else {
  47. ul1.style.display = 'none'
  48. }
  49. }
  50. }
  51. function next(ele) {
  52. do {
  53. ele = ele.nextSibling;
  54. }while(ele.nodeType != 1)
  55. return ele;
  56. }
  57. </script>
  58. </body>
  59. </html>