当前位置:   article > 正文

JavaScript之tab栏切换_js tab切换

js tab切换

本文主要介绍如何通过JS实现tab栏的切换效果,下面介绍五种方式:1.利用自定义属性;2.利用let关键字;3.利用事件对象实现代码复用;4.let关键字封装函数实现代码复用;5.自定义属性封装函数实现代码复用

1.利用自定义属性

此种方法的核心在于给每个tab选项设置一个自定义属性值,然后根据相应的属性值找到对应的内容选项进行显示。代码如下:

  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. a {
  14. text-decoration: none;
  15. }
  16. li {
  17. list-style: none;
  18. }
  19. ul::after {
  20. display: block;
  21. content: '';
  22. clear: both;
  23. }
  24. ul li {
  25. float: left;
  26. margin-right: 5px;
  27. background-color: skyblue;
  28. }
  29. ul li:first-child {
  30. background-color: #ccc;
  31. }
  32. h2 {
  33. text-align: center;
  34. }
  35. ul li a {
  36. display: inline-block;
  37. font-size: 20px;
  38. text-align: center;
  39. width: 200px;
  40. height: 50px;
  41. line-height: 50px;
  42. color: #000;
  43. }
  44. /* 选项区域 */
  45. .optionArea {
  46. position: relative;
  47. }
  48. .optionArea div {
  49. position: absolute;
  50. top: 10px;
  51. width: 1220px;
  52. height: 500px;
  53. background-color: pink;
  54. display: none;
  55. }
  56. .optionArea div:first-child {
  57. display: block;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <ul>
  63. <li><a href="javascript:;">选项一</a></li>
  64. <li><a href="javascript:;">选项二</a></li>
  65. <li><a href="javascript:;">选项三</a></li>
  66. <li><a href="javascript:;">选项四</a></li>
  67. <li><a href="javascript:;">选项五</a></li>
  68. <li><a href="javascript:;">选项六</a></li>
  69. </ul>
  70. <section class="optionArea">
  71. <div>
  72. <h2>标题1</h2>
  73. </div>
  74. <div>
  75. <h2>标题2</h2>
  76. </div>
  77. <div>
  78. <h2>标题3</h2>
  79. </div>
  80. <div>
  81. <h2>标题4</h2>
  82. </div>
  83. <div>
  84. <h2>标题5</h2>
  85. </div>
  86. <div>
  87. <h2>标题6</h2>
  88. </div>
  89. </section>
  90. <script>
  91. var lis = document.querySelectorAll('li');
  92. var divs = document.querySelectorAll('div');
  93. for (let i = 0; i < lis.length; i++) {
  94. document.querySelectorAll('li')[i].setAttribute('index', i);
  95. lis[i].onclick = function() {
  96. for (let i of lis) {
  97. i.style.backgroundColor = 'skyblue';
  98. }
  99. this.style.backgroundColor = '#ccc';
  100. for (let i of divs) {
  101. i.style.display = 'none';
  102. }
  103. divs[this.getAttribute('index')].style.display = 'block';
  104. }
  105. }
  106. </script>
  107. </body>
  108. </html>

2.利用let关键词

此种方法相较于第一种更加简单,利用let提供的块作用域特点,直接利用各个选项的索引值即可获取相应的内容进行显示。代码如下:

  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. a {
  14. text-decoration: none;
  15. }
  16. li {
  17. list-style: none;
  18. }
  19. ul::after {
  20. display: block;
  21. content: '';
  22. clear: both;
  23. }
  24. ul li {
  25. float: left;
  26. margin-right: 5px;
  27. background-color: skyblue;
  28. }
  29. ul li:first-child {
  30. background-color: #ccc;
  31. }
  32. h2 {
  33. text-align: center;
  34. }
  35. ul li a {
  36. display: inline-block;
  37. font-size: 20px;
  38. text-align: center;
  39. width: 200px;
  40. height: 50px;
  41. line-height: 50px;
  42. color: #000;
  43. }
  44. /* 选项区域 */
  45. .optionArea {
  46. position: relative;
  47. }
  48. .optionArea div {
  49. position: absolute;
  50. top: 10px;
  51. width: 1220px;
  52. height: 500px;
  53. background-color: pink;
  54. display: none;
  55. }
  56. .optionArea div:first-child {
  57. display: block;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <ul>
  63. <li><a href="javascript:;">选项一</a></li>
  64. <li><a href="javascript:;">选项二</a></li>
  65. <li><a href="javascript:;">选项三</a></li>
  66. <li><a href="javascript:;">选项四</a></li>
  67. <li><a href="javascript:;">选项五</a></li>
  68. <li><a href="javascript:;">选项六</a></li>
  69. </ul>
  70. <section class="optionArea">
  71. <div>
  72. <h2>标题1</h2>
  73. </div>
  74. <div>
  75. <h2>标题2</h2>
  76. </div>
  77. <div>
  78. <h2>标题3</h2>
  79. </div>
  80. <div>
  81. <h2>标题4</h2>
  82. </div>
  83. <div>
  84. <h2>标题5</h2>
  85. </div>
  86. <div>
  87. <h2>标题6</h2>
  88. </div>
  89. </section>
  90. <script>
  91. var lis = document.querySelectorAll('li');
  92. var divs = document.querySelectorAll('div');
  93. for (let i = 0; i < lis.length; i++) {
  94. lis[i].onclick = function() {
  95. for (let i of lis) {
  96. i.style.backgroundColor = 'skyblue';
  97. }
  98. this.style.backgroundColor = '#ccc';
  99. for (let i of divs) {
  100. i.style.display = 'none';
  101. }
  102. divs[i].style.display = 'block';
  103. }
  104. }
  105. </script>
  106. </body>
  107. </html>

3.利用事件对象。前两种方法所做出来的tab栏是单一的,若要进行代码的复用,则会出现问题。利用事件对象可使不同的tab栏互不影响,达到代码复用的效果,从而提高开发效率。代码如下:

  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. a {
  14. text-decoration: none;
  15. }
  16. li {
  17. list-style: none;
  18. }
  19. .tab .ul::after {
  20. display: block;
  21. content: '';
  22. clear: both;
  23. }
  24. .tab .ul a {
  25. float: left;
  26. margin-right: 5px;
  27. background-color: skyblue;
  28. font-size: 20px;
  29. text-align: center;
  30. width: 200px;
  31. height: 50px;
  32. line-height: 50px;
  33. color: #000;
  34. }
  35. .bc {
  36. background-color: #ccc !important;
  37. }
  38. .tab h2 {
  39. text-align: center;
  40. }
  41. .optionArea div:nth-child(n+2) {
  42. display: none;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="tab">
  48. <div class="ul">
  49. <a href="javascript:;" class="bc">选项一</a>
  50. <a href="javascript:;">选项二</a>
  51. <a href="javascript:;">选项三</a>
  52. </div>
  53. <section class="optionArea">
  54. <div>
  55. 标题1
  56. </div>
  57. <div>
  58. 标题2
  59. </div>
  60. <div>
  61. 标题3
  62. </div>
  63. </section>
  64. </div>
  65. <div class="tab">
  66. <div class="ul">
  67. <a href="javascript:;" class="bc">选项一</a>
  68. <a href="javascript:;">选项二</a>
  69. <a href="javascript:;">选项三</a>
  70. </div>
  71. <section class="optionArea">
  72. <div>
  73. 标题1
  74. </div>
  75. <div>
  76. 标题2
  77. </div>
  78. <div>
  79. 标题3
  80. </div>
  81. </section>
  82. </div>
  83. <div class="tab">
  84. <div class="ul">
  85. <a href="javascript:;" class="bc">选项一</a>
  86. <a href="javascript:;">选项二</a>
  87. <a href="javascript:;">选项三</a>
  88. </div>
  89. <section class="optionArea">
  90. <div>
  91. 标题1
  92. </div>
  93. <div>
  94. 标题2
  95. </div>
  96. <div>
  97. 标题3
  98. </div>
  99. </section>
  100. </div>
  101. <div class="tab">
  102. <div class="ul">
  103. <a href="javascript:;" class="bc">选项一</a>
  104. <a href="javascript:;">选项二</a>
  105. <a href="javascript:;">选项三</a>
  106. </div>
  107. <section class="optionArea">
  108. <div>
  109. 标题1
  110. </div>
  111. <div>
  112. 标题2
  113. </div>
  114. <div>
  115. 标题3
  116. </div>
  117. </section>
  118. </div>
  119. <script>
  120. var uls = document.querySelectorAll('.ul');
  121. for (let i of uls) {
  122. i.addEventListener('click', function(e) {
  123. var as = e.target.parentNode.children;
  124. for (let i = 0; i < as.length; i++) {
  125. as[i].className = '';
  126. as[i].setAttribute('index', i);
  127. }
  128. e.target.className = 'bc';
  129. // 底下的标题先全部隐藏
  130. var divs = e.target.parentNode.nextElementSibling.children;
  131. for (let i of divs) {
  132. i.style.display = 'none';
  133. }
  134. // 显示对应的标题
  135. divs[e.target.getAttribute('index')].style.display = 'block';
  136. })
  137. }
  138. </script>
  139. </body>
  140. </html>

4.第四种方法实际上将第二种方法进行封装,利用函数的局部作用域及let提供的块级作用域实现tab的复用。

  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. a {
  14. text-decoration: none;
  15. }
  16. li {
  17. list-style: none;
  18. }
  19. .tab .ul::after {
  20. display: block;
  21. content: '';
  22. clear: both;
  23. }
  24. .tab .ul a {
  25. float: left;
  26. margin-right: 5px;
  27. background-color: skyblue;
  28. font-size: 20px;
  29. text-align: center;
  30. width: 200px;
  31. height: 50px;
  32. line-height: 50px;
  33. color: #000;
  34. }
  35. .bc {
  36. background-color: #ccc !important;
  37. }
  38. .tab h2 {
  39. text-align: center;
  40. }
  41. .optionArea div:nth-child(n+2) {
  42. display: none;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="tab">
  48. <div class="ul">
  49. <a href="javascript:;" class="bc">选项一</a>
  50. <a href="javascript:;">选项二</a>
  51. <a href="javascript:;">选项三</a>
  52. </div>
  53. <section class="optionArea">
  54. <div>
  55. 标题1
  56. </div>
  57. <div>
  58. 标题2
  59. </div>
  60. <div>
  61. 标题3
  62. </div>
  63. </section>
  64. </div>
  65. <div class="tab">
  66. <div class="ul">
  67. <a href="javascript:;" class="bc">选项一</a>
  68. <a href="javascript:;">选项二</a>
  69. <a href="javascript:;">选项三</a>
  70. </div>
  71. <section class="optionArea">
  72. <div>
  73. 标题1
  74. </div>
  75. <div>
  76. 标题2
  77. </div>
  78. <div>
  79. 标题3
  80. </div>
  81. </section>
  82. </div>
  83. <div class="tab">
  84. <div class="ul">
  85. <a href="javascript:;" class="bc">选项一</a>
  86. <a href="javascript:;">选项二</a>
  87. <a href="javascript:;">选项三</a>
  88. </div>
  89. <section class="optionArea">
  90. <div>
  91. 标题1
  92. </div>
  93. <div>
  94. 标题2
  95. </div>
  96. <div>
  97. 标题3
  98. </div>
  99. </section>
  100. </div>
  101. <script>
  102. var tabs = document.querySelectorAll('.tab');
  103. for (let i of tabs) {
  104. tabChange(i);
  105. }
  106. function tabChange(e) {
  107. var as = e.querySelectorAll('a');
  108. var divs = e.querySelectorAll('section div');
  109. for (let i in as) {
  110. as[i].onclick = function() {
  111. for (let i of as) {
  112. i.className = '';
  113. }
  114. this.className = 'bc';
  115. for (let i of divs) {
  116. i.style.display = 'none';
  117. }
  118. divs[i].style.display = 'block';
  119. }
  120. }
  121. }
  122. </script>
  123. </body>
  124. </html>

5.第五种方法未使用let关键词,主要根据创建的自定义属性实现效果。

  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. a {
  14. text-decoration: none;
  15. }
  16. li {
  17. list-style: none;
  18. }
  19. .tab .ul::after {
  20. display: block;
  21. content: '';
  22. clear: both;
  23. }
  24. .tab .ul a {
  25. float: left;
  26. margin-right: 5px;
  27. background-color: skyblue;
  28. font-size: 20px;
  29. text-align: center;
  30. width: 200px;
  31. height: 50px;
  32. line-height: 50px;
  33. color: #000;
  34. }
  35. .bc {
  36. background-color: #ccc !important;
  37. }
  38. .tab h2 {
  39. text-align: center;
  40. }
  41. .optionArea div:nth-child(n+2) {
  42. display: none;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="tab">
  48. <div class="ul">
  49. <a href="javascript:;" class="bc">选项一</a>
  50. <a href="javascript:;">选项二</a>
  51. <a href="javascript:;">选项三</a>
  52. </div>
  53. <section class="optionArea">
  54. <div>
  55. 标题1
  56. </div>
  57. <div>
  58. 标题2
  59. </div>
  60. <div>
  61. 标题3
  62. </div>
  63. </section>
  64. </div>
  65. <div class="tab">
  66. <div class="ul">
  67. <a href="javascript:;" class="bc">选项一</a>
  68. <a href="javascript:;">选项二</a>
  69. <a href="javascript:;">选项三</a>
  70. </div>
  71. <section class="optionArea">
  72. <div>
  73. 标题1
  74. </div>
  75. <div>
  76. 标题2
  77. </div>
  78. <div>
  79. 标题3
  80. </div>
  81. </section>
  82. </div>
  83. <div class="tab">
  84. <div class="ul">
  85. <a href="javascript:;" class="bc">选项一</a>
  86. <a href="javascript:;">选项二</a>
  87. <a href="javascript:;">选项三</a>
  88. </div>
  89. <section class="optionArea">
  90. <div>
  91. 标题1
  92. </div>
  93. <div>
  94. 标题2
  95. </div>
  96. <div>
  97. 标题3
  98. </div>
  99. </section>
  100. </div>
  101. <script>
  102. var tabs = document.querySelectorAll('.tab');
  103. for (let i of tabs) {
  104. tabChange(i);
  105. }
  106. function tabChange(e) {
  107. var as = e.querySelectorAll('a');
  108. var divs = e.querySelectorAll('section div');
  109. for (var i = 0; i < as.length; i++) {
  110. // 可以通过节点的“.”直接创建自定义属性,但不会显示出来
  111. as[i].location = i;
  112. as[i].onclick = function() {
  113. for (let i of as) {
  114. i.className = '';
  115. }
  116. this.className = 'bc';
  117. for (let i of divs) {
  118. i.style.display = 'none';
  119. }
  120. divs[this.location].style.display = 'block';
  121. }
  122. }
  123. }
  124. </script>
  125. </body>
  126. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/69354
推荐阅读
相关标签
  

闽ICP备14008679号