11_垂直导航.html 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // console.log(h2.nextElementSibling)
  45. for(var i=0;i<h2.length;i++){
  46. h2[i].onclick = function(){
  47. var ul1 = next(this)
  48. console.log(ul1)
  49. if(ul1.style.display == 'block'){
  50. ul1.style.display = 'none'
  51. } else {
  52. ul1.style.display = 'block'
  53. }
  54. }
  55. }
  56. function next(elem){
  57. do{
  58. elem = elem.nextSibling
  59. } while (elem.nodeType != 1)
  60. return elem
  61. }
  62. </script>
  63. </body>
  64. </html>