17.垂直导航.html 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }
  13. h2 {
  14. width: 200px;
  15. height: 80px;
  16. text-align: center;
  17. line-height: 80px;
  18. background: purple;
  19. color: #fff;
  20. margin-top: 15px;
  21. }
  22. ul {
  23. display: none;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container">
  29. <h2>管理区</h2>
  30. <ul>
  31. <li>我的项目1</li>
  32. <li>我的项目2</li>
  33. <li>我的项目3</li>
  34. <li>我的项目4</li>
  35. </ul>
  36. <h2>提交区</h2>
  37. <ul>
  38. <li>我的内容1</li>
  39. <li>我的内容2</li>
  40. <li>我的内容3</li>
  41. <li>我的内容4</li>
  42. </ul>
  43. </div>
  44. <script>
  45. var h2 = document.querySelectorAll("h2");
  46. console.log(this); // this 在全局 指向windows
  47. for(var i=0;i<h2.length;i++) {
  48. h2[i].onclick = function() {
  49. // this在点击事件与类似事件 执行当前对象
  50. console.log(this); // h2
  51. var uls = this.nextElementSibling;
  52. if(uls.style.display == 'none') {
  53. uls.style.display = 'block';
  54. } else {
  55. uls.style.display = 'none';
  56. }
  57. }
  58. }
  59. </script>
  60. </body>
  61. </html>