javascript—为什么在我使用for of循环修改innertext时,span元素的innertext没有更改?

nkhmeac6  于 2021-09-23  发布在  Java
关注(0)|答案(3)|浏览(538)
  1. const colors = ["green", "red", "rgba(133,122,200)", "#f15025"];
  2. const colorIdentifier = document.querySelector('.color');
  3. const button = document.getElementById('btn');
  4. for (let color of colors) {
  5. loopButton();
  6. }
  7. function loopButton() {
  8. button.addEventListener('click', function() {
  9. colorIdentifier.innerText = color;
  10. })
  11. };
  1. /* * {
  2. border: 5px solid purple;
  3. } */
  4. /*
  5. ===============
  6. Fonts
  7. ===============
  8. * /
  9. @import url("https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,700&display=swap");
  10. /*
  11. ===============
  12. Variables
  13. ===============
  14. * /
  15. :root {
  16. /* dark shades of primary color*/
  17. --clr-primary-1: hsl(205, 86%, 17%);
  18. --clr-primary-2: hsl(205, 77%, 27%);
  19. --clr-primary-3: hsl(205, 72%, 37%);
  20. --clr-primary-4: hsl(205, 63%, 48%);
  21. /* primary/main color */
  22. --clr-primary-5: hsl(205, 78%, 60%);
  23. /* lighter shades of primary color */
  24. --clr-primary-6: hsl(205, 89%, 70%);
  25. --clr-primary-7: hsl(205, 90%, 76%);
  26. --clr-primary-8: hsl(205, 86%, 81%);
  27. --clr-primary-9: hsl(205, 90%, 88%);
  28. --clr-primary-10: hsl(205, 100%, 96%);
  29. /* darkest grey - used for headings */
  30. --clr-grey-1: hsl(209, 61%, 16%);
  31. --clr-grey-2: hsl(211, 39%, 23%);
  32. --clr-grey-3: hsl(209, 34%, 30%);
  33. --clr-grey-4: hsl(209, 28%, 39%);
  34. /* grey used for paragraphs */
  35. --clr-grey-5: hsl(210, 22%, 49%);
  36. --clr-grey-6: hsl(209, 23%, 60%);
  37. --clr-grey-7: hsl(211, 27%, 70%);
  38. --clr-grey-8: hsl(210, 31%, 80%);
  39. --clr-grey-9: hsl(212, 33%, 89%);
  40. --clr-grey-10: hsl(210, 36%, 96%);
  41. --clr-white: #fff;
  42. --clr-red-dark: hsl(360, 67%, 44%);
  43. --clr-red-light: hsl(360, 71%, 66%);
  44. --clr-green-dark: hsl(125, 67%, 44%);
  45. --clr-green-light: hsl(125, 71%, 66%);
  46. --clr-black: #222;
  47. --ff-primary: "Roboto", sans-serif;
  48. --ff-secondary: "Open Sans", sans-serif;
  49. --transition: all 0.3s linear;
  50. --spacing: 0.1rem;
  51. --radius: 0.25rem;
  52. --light-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  53. --dark-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  54. --max-width: 1170px;
  55. --fixed-width: 620px;
  56. }
  57. /*
  58. ===============
  59. Global Styles
  60. ===============
  61. * /
  62. * ,
  63. ::after,
  64. ::before {
  65. margin: 0;
  66. padding: 0;
  67. box-sizing: border-box;
  68. }
  69. body {
  70. font-family: var(--ff-secondary);
  71. background: var(--clr-grey-10);
  72. color: var(--clr-grey-1);
  73. line-height: 1.5;
  74. font-size: 0.875rem;
  75. }
  76. ul {
  77. list-style-type: none;
  78. }
  79. a {
  80. text-decoration: none;
  81. }
  82. h1,
  83. h2,
  84. h3,
  85. h4 {
  86. letter-spacing: var(--spacing);
  87. text-transform: capitalize;
  88. line-height: 1.25;
  89. margin-bottom: 0.75rem;
  90. font-family: var(--ff-primary);
  91. }
  92. h1 {
  93. font-size: 3rem;
  94. }
  95. h2 {
  96. font-size: 2rem;
  97. }
  98. h3 {
  99. font-size: 1.25rem;
  100. }
  101. h4 {
  102. font-size: 0.875rem;
  103. }
  104. p {
  105. margin-bottom: 1.25rem;
  106. color: var(--clr-grey-5);
  107. }
  108. @media screen and (min-width: 800px) {
  109. h1 {
  110. font-size: 4rem;
  111. }
  112. h2 {
  113. font-size: 2.5rem;
  114. }
  115. h3 {
  116. font-size: 1.75rem;
  117. }
  118. h4 {
  119. font-size: 1rem;
  120. }
  121. body {
  122. font-size: 1rem;
  123. }
  124. h1,
  125. h2,
  126. h3,
  127. h4 {
  128. line-height: 1;
  129. }
  130. }
  131. /* global classes */
  132. /* section */
  133. .section {
  134. padding: 5rem 0;
  135. }
  136. .section-center {
  137. width: 90vw;
  138. margin: 0 auto;
  139. max-width: 1170px;
  140. }
  141. @media screen and (min-width: 992px) {
  142. .section-center {
  143. width: 95vw;
  144. }
  145. }
  146. main {
  147. min-height: 100vh;
  148. display: grid;
  149. place-items: center;
  150. }
  151. /*
  152. ===============
  153. Nav
  154. ===============
  155. * /
  156. nav {
  157. background: var(--clr-white);
  158. height: 3rem;
  159. display: grid;
  160. align-items: center;
  161. box-shadow: var(--dark-shadow);
  162. }
  163. .nav-center {
  164. width: 90vw;
  165. max-width: var(--fixed-width);
  166. margin: 0 auto;
  167. display: flex;
  168. align-items: center;
  169. justify-content: space-between;
  170. }
  171. .nav-center h4 {
  172. margin-bottom: 0;
  173. color: var(--clr-primary-5);
  174. }
  175. .nav-links {
  176. display: flex;
  177. }
  178. nav a {
  179. text-transform: capitalize;
  180. font-weight: 700;
  181. font-size: 1rem;
  182. color: var(--clr-primary-1);
  183. letter-spacing: var(--spacing);
  184. margin-right: 1rem;
  185. }
  186. nav a:hover {
  187. color: var(--clr-primary-5);
  188. }
  189. /*
  190. ===============
  191. Container
  192. ===============
  193. * /
  194. main {
  195. min-height: calc(100vh - 3rem);
  196. display: grid;
  197. place-items: center;
  198. }
  199. .container {
  200. text-align: center;
  201. }
  202. .container h2 {
  203. background: var(--clr-black);
  204. color: var(--clr-white);
  205. padding: 1rem;
  206. border-radius: var(--radius);
  207. margin-bottom: 2.5rem;
  208. }
  209. .color {
  210. color: var(--clr-primary-5);
  211. }
  212. .btn-hero {
  213. font-family: var(--ff-primary);
  214. text-transform: uppercase;
  215. background: transparent;
  216. color: var(--clr-black);
  217. letter-spacing: var(--spacing);
  218. display: inline-block;
  219. font-weight: 700;
  220. transition: var(--transition);
  221. border: 2px solid var(--clr-black);
  222. cursor: pointer;
  223. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  224. border-radius: var(--radius);
  225. font-size: 1rem;
  226. padding: 0.75rem 1.25rem;
  227. }
  228. .btn-hero:hover {
  229. color: var(--clr-white);
  230. background: var(--clr-black);
  231. }
  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>Color Flipper || Simple</title>
  7. <!-- styles -->
  8. <link rel="stylesheet" href="styles.css" />
  9. </head>
  10. <body>
  11. <nav>
  12. <div class="nav-center">
  13. <h4>Color Flipper</h4>
  14. <ul class="nav-links">
  15. <li><a href="index.html">simple</a></li>
  16. <li><a href="hex.html">hex</a></li>
  17. </ul>
  18. </div>
  19. </nav>
  20. <main>
  21. <div class="container">
  22. <h2>background color: <span class="color">#000</span></h2>
  23. <button class="btn btn-hero" id="btn">click me</button>
  24. </div>
  25. </main>
  26. <!-- javascript -->
  27. <script src="app.js"></script>
  28. </body>
  29. </html>

我想修改html的内部文本 span 元素。并且内部文本显示不同颜色的 colors 后按顺序排列 button 元素被单击。

  1. <h2>background color: <span class="color">#000</span></h2>

我使用了循环的方法。

  1. for(let color of colors) {
  2. loopButton();
  3. }
  4. function loopButton() { button.addEventListener('click', function() {
  5. colorIdentifier.innerText = color;
  6. }
  7. )};

我想知道,为什么会说“虽然我使用了snippet,但我的帖子看起来大部分都是代码?”。我认为如果我在代码片段中发布大量代码,会很有用,因为人们可以看到完整的代码。

uhry853o

uhry853o1#

因为颜色是在另一个函数上定义的,所以您不能访问它,因为let和const是块链变量-它们可以是acces

  1. const colors = ["green", "red", "rgba(133,122,200)", "#f15025"];
  2. const colorIdentifier = document.querySelector('.color');
  3. const button = document.getElementById('btn');
  4. let i = 0;
  5. function previewColors() {
  6. colorIdentifier.innerHTML = colors[i]; // Change innerHTML text
  7. colorIdentifier.style.color = colors[i]; // Change innerHTML color
  8. i++;
  9. if( i < colors.length){
  10. window.setTimeout(previewColors, 1000); // Access array after 1 sec if i < color.length - LOOPS over the array in recursive way- .
  11. }
  12. }
  13. button.addEventListener('click', previewColors);
  1. /* * {
  2. border: 5px solid purple;
  3. } */
  4. /*
  5. ===============
  6. Fonts
  7. ===============
  8. * /
  9. @import url("https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,700&display=swap");
  10. /*
  11. ===============
  12. Variables
  13. ===============
  14. * /
  15. :root {
  16. /* dark shades of primary color*/
  17. --clr-primary-1: hsl(205, 86%, 17%);
  18. --clr-primary-2: hsl(205, 77%, 27%);
  19. --clr-primary-3: hsl(205, 72%, 37%);
  20. --clr-primary-4: hsl(205, 63%, 48%);
  21. /* primary/main color */
  22. --clr-primary-5: hsl(205, 78%, 60%);
  23. /* lighter shades of primary color */
  24. --clr-primary-6: hsl(205, 89%, 70%);
  25. --clr-primary-7: hsl(205, 90%, 76%);
  26. --clr-primary-8: hsl(205, 86%, 81%);
  27. --clr-primary-9: hsl(205, 90%, 88%);
  28. --clr-primary-10: hsl(205, 100%, 96%);
  29. /* darkest grey - used for headings */
  30. --clr-grey-1: hsl(209, 61%, 16%);
  31. --clr-grey-2: hsl(211, 39%, 23%);
  32. --clr-grey-3: hsl(209, 34%, 30%);
  33. --clr-grey-4: hsl(209, 28%, 39%);
  34. /* grey used for paragraphs */
  35. --clr-grey-5: hsl(210, 22%, 49%);
  36. --clr-grey-6: hsl(209, 23%, 60%);
  37. --clr-grey-7: hsl(211, 27%, 70%);
  38. --clr-grey-8: hsl(210, 31%, 80%);
  39. --clr-grey-9: hsl(212, 33%, 89%);
  40. --clr-grey-10: hsl(210, 36%, 96%);
  41. --clr-white: #fff;
  42. --clr-red-dark: hsl(360, 67%, 44%);
  43. --clr-red-light: hsl(360, 71%, 66%);
  44. --clr-green-dark: hsl(125, 67%, 44%);
  45. --clr-green-light: hsl(125, 71%, 66%);
  46. --clr-black: #222;
  47. --ff-primary: "Roboto", sans-serif;
  48. --ff-secondary: "Open Sans", sans-serif;
  49. --transition: all 0.3s linear;
  50. --spacing: 0.1rem;
  51. --radius: 0.25rem;
  52. --light-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  53. --dark-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  54. --max-width: 1170px;
  55. --fixed-width: 620px;
  56. }
  57. /*
  58. ===============
  59. Global Styles
  60. ===============
  61. * /
  62. * ,
  63. ::after,
  64. ::before {
  65. margin: 0;
  66. padding: 0;
  67. box-sizing: border-box;
  68. }
  69. body {
  70. font-family: var(--ff-secondary);
  71. background: var(--clr-grey-10);
  72. color: var(--clr-grey-1);
  73. line-height: 1.5;
  74. font-size: 0.875rem;
  75. }
  76. ul {
  77. list-style-type: none;
  78. }
  79. a {
  80. text-decoration: none;
  81. }
  82. h1,
  83. h2,
  84. h3,
  85. h4 {
  86. letter-spacing: var(--spacing);
  87. text-transform: capitalize;
  88. line-height: 1.25;
  89. margin-bottom: 0.75rem;
  90. font-family: var(--ff-primary);
  91. }
  92. h1 {
  93. font-size: 3rem;
  94. }
  95. h2 {
  96. font-size: 2rem;
  97. }
  98. h3 {
  99. font-size: 1.25rem;
  100. }
  101. h4 {
  102. font-size: 0.875rem;
  103. }
  104. p {
  105. margin-bottom: 1.25rem;
  106. color: var(--clr-grey-5);
  107. }
  108. @media screen and (min-width: 800px) {
  109. h1 {
  110. font-size: 4rem;
  111. }
  112. h2 {
  113. font-size: 2.5rem;
  114. }
  115. h3 {
  116. font-size: 1.75rem;
  117. }
  118. h4 {
  119. font-size: 1rem;
  120. }
  121. body {
  122. font-size: 1rem;
  123. }
  124. h1,
  125. h2,
  126. h3,
  127. h4 {
  128. line-height: 1;
  129. }
  130. }
  131. /* global classes */
  132. /* section */
  133. .section {
  134. padding: 5rem 0;
  135. }
  136. .section-center {
  137. width: 90vw;
  138. margin: 0 auto;
  139. max-width: 1170px;
  140. }
  141. @media screen and (min-width: 992px) {
  142. .section-center {
  143. width: 95vw;
  144. }
  145. }
  146. main {
  147. min-height: 100vh;
  148. display: grid;
  149. place-items: center;
  150. }
  151. /*
  152. ===============
  153. Nav
  154. ===============
  155. * /
  156. nav {
  157. background: var(--clr-white);
  158. height: 3rem;
  159. display: grid;
  160. align-items: center;
  161. box-shadow: var(--dark-shadow);
  162. }
  163. .nav-center {
  164. width: 90vw;
  165. max-width: var(--fixed-width);
  166. margin: 0 auto;
  167. display: flex;
  168. align-items: center;
  169. justify-content: space-between;
  170. }
  171. .nav-center h4 {
  172. margin-bottom: 0;
  173. color: var(--clr-primary-5);
  174. }
  175. .nav-links {
  176. display: flex;
  177. }
  178. nav a {
  179. text-transform: capitalize;
  180. font-weight: 700;
  181. font-size: 1rem;
  182. color: var(--clr-primary-1);
  183. letter-spacing: var(--spacing);
  184. margin-right: 1rem;
  185. }
  186. nav a:hover {
  187. color: var(--clr-primary-5);
  188. }
  189. /*
  190. ===============
  191. Container
  192. ===============
  193. * /
  194. main {
  195. min-height: calc(100vh - 3rem);
  196. display: grid;
  197. place-items: center;
  198. }
  199. .container {
  200. text-align: center;
  201. }
  202. .container h2 {
  203. background: var(--clr-black);
  204. color: var(--clr-white);
  205. padding: 1rem;
  206. border-radius: var(--radius);
  207. margin-bottom: 2.5rem;
  208. }
  209. .color {
  210. color: var(--clr-primary-5);
  211. }
  212. .btn-hero {
  213. font-family: var(--ff-primary);
  214. text-transform: uppercase;
  215. background: transparent;
  216. color: var(--clr-black);
  217. letter-spacing: var(--spacing);
  218. display: inline-block;
  219. font-weight: 700;
  220. transition: var(--transition);
  221. border: 2px solid var(--clr-black);
  222. cursor: pointer;
  223. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  224. border-radius: var(--radius);
  225. font-size: 1rem;
  226. padding: 0.75rem 1.25rem;
  227. }
  228. .btn-hero:hover {
  229. color: var(--clr-white);
  230. background: var(--clr-black);
  231. }
  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>Color Flipper || Simple</title>
  7. <!-- styles -->
  8. <link rel="stylesheet" href="styles.css" />
  9. </head>
  10. <body>
  11. <nav>
  12. <div class="nav-center">
  13. <h4>Color Flipper</h4>
  14. <ul class="nav-links">
  15. <li><a href="index.html">simple</a></li>
  16. <li><a href="hex.html">hex</a></li>
  17. </ul>
  18. </div>
  19. </nav>
  20. <main>
  21. <div class="container">
  22. <h2>background color: <span class="color">#000</span></h2>
  23. <button class="btn btn-hero" id="btn">click me</button>
  24. </div>
  25. </main>
  26. <!-- javascript -->
  27. <script src="app.js"></script>
  28. </body>
  29. </html>
展开查看全部
jv4diomz

jv4diomz2#

首先,你得到 Uncaught reference 错误,因为事件侦听器函数是在您添加它的函数上下文之外执行的,因此它没有访问变量的权限。一个简单的解决方法是将其传递:

  1. for (const color of colors) {
  2. loopButton(color);
  3. }

至于颜色的实际循环,您将向同一个颜色添加多个回调 click 事件和元素,所以您实际上是在覆盖它们。有几种方法可以实现您想要的,但我建议在代码中保留一个计数器,并在每次按下按钮时递增它。这就使得 color 每次调用回调时,变量都有不同的值。比如:

  1. let counter = 0;
  2. function callBack() {
  3. const idx = counter % (colors.length);
  4. colorIdentifier.innerText = colors[idx];
  5. colorIdentifier.style.color = colors[idx];
  6. counter++;
  7. }
  8. button.addEventListener('click', callBack);

您将在下面找到现在的功能片段。希望我已经把事情说清楚了。:)

  1. const colors = ["green", "red", "rgba(133,122,200)", "#f15025"];
  2. const colorIdentifier = document.querySelector('.color');
  3. const button = document.getElementById('btn');
  4. let counter = 0;
  5. function callBack() {
  6. const idx = counter % (colors.length);
  7. colorIdentifier.innerText = colors[idx];
  8. colorIdentifier.style.color = colors[idx];
  9. counter++;
  10. }
  11. button.addEventListener('click', callBack);
  1. /* * {
  2. border: 5px solid purple;
  3. } */
  4. /*
  5. ===============
  6. Fonts
  7. ===============
  8. * /
  9. @import url("https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,700&display=swap");
  10. /*
  11. ===============
  12. Variables
  13. ===============
  14. * /
  15. :root {
  16. /* dark shades of primary color*/
  17. --clr-primary-1: hsl(205, 86%, 17%);
  18. --clr-primary-2: hsl(205, 77%, 27%);
  19. --clr-primary-3: hsl(205, 72%, 37%);
  20. --clr-primary-4: hsl(205, 63%, 48%);
  21. /* primary/main color */
  22. --clr-primary-5: hsl(205, 78%, 60%);
  23. /* lighter shades of primary color */
  24. --clr-primary-6: hsl(205, 89%, 70%);
  25. --clr-primary-7: hsl(205, 90%, 76%);
  26. --clr-primary-8: hsl(205, 86%, 81%);
  27. --clr-primary-9: hsl(205, 90%, 88%);
  28. --clr-primary-10: hsl(205, 100%, 96%);
  29. /* darkest grey - used for headings */
  30. --clr-grey-1: hsl(209, 61%, 16%);
  31. --clr-grey-2: hsl(211, 39%, 23%);
  32. --clr-grey-3: hsl(209, 34%, 30%);
  33. --clr-grey-4: hsl(209, 28%, 39%);
  34. /* grey used for paragraphs */
  35. --clr-grey-5: hsl(210, 22%, 49%);
  36. --clr-grey-6: hsl(209, 23%, 60%);
  37. --clr-grey-7: hsl(211, 27%, 70%);
  38. --clr-grey-8: hsl(210, 31%, 80%);
  39. --clr-grey-9: hsl(212, 33%, 89%);
  40. --clr-grey-10: hsl(210, 36%, 96%);
  41. --clr-white: #fff;
  42. --clr-red-dark: hsl(360, 67%, 44%);
  43. --clr-red-light: hsl(360, 71%, 66%);
  44. --clr-green-dark: hsl(125, 67%, 44%);
  45. --clr-green-light: hsl(125, 71%, 66%);
  46. --clr-black: #222;
  47. --ff-primary: "Roboto", sans-serif;
  48. --ff-secondary: "Open Sans", sans-serif;
  49. --transition: all 0.3s linear;
  50. --spacing: 0.1rem;
  51. --radius: 0.25rem;
  52. --light-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  53. --dark-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  54. --max-width: 1170px;
  55. --fixed-width: 620px;
  56. }
  57. /*
  58. ===============
  59. Global Styles
  60. ===============
  61. * /
  62. * ,
  63. ::after,
  64. ::before {
  65. margin: 0;
  66. padding: 0;
  67. box-sizing: border-box;
  68. }
  69. body {
  70. font-family: var(--ff-secondary);
  71. background: var(--clr-grey-10);
  72. color: var(--clr-grey-1);
  73. line-height: 1.5;
  74. font-size: 0.875rem;
  75. }
  76. ul {
  77. list-style-type: none;
  78. }
  79. a {
  80. text-decoration: none;
  81. }
  82. h1,
  83. h2,
  84. h3,
  85. h4 {
  86. letter-spacing: var(--spacing);
  87. text-transform: capitalize;
  88. line-height: 1.25;
  89. margin-bottom: 0.75rem;
  90. font-family: var(--ff-primary);
  91. }
  92. h1 {
  93. font-size: 3rem;
  94. }
  95. h2 {
  96. font-size: 2rem;
  97. }
  98. h3 {
  99. font-size: 1.25rem;
  100. }
  101. h4 {
  102. font-size: 0.875rem;
  103. }
  104. p {
  105. margin-bottom: 1.25rem;
  106. color: var(--clr-grey-5);
  107. }
  108. @media screen and (min-width: 800px) {
  109. h1 {
  110. font-size: 4rem;
  111. }
  112. h2 {
  113. font-size: 2.5rem;
  114. }
  115. h3 {
  116. font-size: 1.75rem;
  117. }
  118. h4 {
  119. font-size: 1rem;
  120. }
  121. body {
  122. font-size: 1rem;
  123. }
  124. h1,
  125. h2,
  126. h3,
  127. h4 {
  128. line-height: 1;
  129. }
  130. }
  131. /* global classes */
  132. /* section */
  133. .section {
  134. padding: 5rem 0;
  135. }
  136. .section-center {
  137. width: 90vw;
  138. margin: 0 auto;
  139. max-width: 1170px;
  140. }
  141. @media screen and (min-width: 992px) {
  142. .section-center {
  143. width: 95vw;
  144. }
  145. }
  146. main {
  147. min-height: 100vh;
  148. display: grid;
  149. place-items: center;
  150. }
  151. /*
  152. ===============
  153. Nav
  154. ===============
  155. * /
  156. nav {
  157. background: var(--clr-white);
  158. height: 3rem;
  159. display: grid;
  160. align-items: center;
  161. box-shadow: var(--dark-shadow);
  162. }
  163. .nav-center {
  164. width: 90vw;
  165. max-width: var(--fixed-width);
  166. margin: 0 auto;
  167. display: flex;
  168. align-items: center;
  169. justify-content: space-between;
  170. }
  171. .nav-center h4 {
  172. margin-bottom: 0;
  173. color: var(--clr-primary-5);
  174. }
  175. .nav-links {
  176. display: flex;
  177. }
  178. nav a {
  179. text-transform: capitalize;
  180. font-weight: 700;
  181. font-size: 1rem;
  182. color: var(--clr-primary-1);
  183. letter-spacing: var(--spacing);
  184. margin-right: 1rem;
  185. }
  186. nav a:hover {
  187. color: var(--clr-primary-5);
  188. }
  189. /*
  190. ===============
  191. Container
  192. ===============
  193. * /
  194. main {
  195. min-height: calc(100vh - 3rem);
  196. display: grid;
  197. place-items: center;
  198. }
  199. .container {
  200. text-align: center;
  201. }
  202. .container h2 {
  203. background: var(--clr-black);
  204. color: var(--clr-white);
  205. padding: 1rem;
  206. border-radius: var(--radius);
  207. margin-bottom: 2.5rem;
  208. }
  209. .color {
  210. color: var(--clr-primary-5);
  211. }
  212. .btn-hero {
  213. font-family: var(--ff-primary);
  214. text-transform: uppercase;
  215. background: transparent;
  216. color: var(--clr-black);
  217. letter-spacing: var(--spacing);
  218. display: inline-block;
  219. font-weight: 700;
  220. transition: var(--transition);
  221. border: 2px solid var(--clr-black);
  222. cursor: pointer;
  223. box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
  224. border-radius: var(--radius);
  225. font-size: 1rem;
  226. padding: 0.75rem 1.25rem;
  227. }
  228. .btn-hero:hover {
  229. color: var(--clr-white);
  230. background: var(--clr-black);
  231. }
  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>Color Flipper || Simple</title>
  7. <!-- styles -->
  8. <link rel="stylesheet" href="styles.css" />
  9. </head>
  10. <body>
  11. <nav>
  12. <div class="nav-center">
  13. <h4>Color Flipper</h4>
  14. <ul class="nav-links">
  15. <li><a href="index.html">simple</a></li>
  16. <li><a href="hex.html">hex</a></li>
  17. </ul>
  18. </div>
  19. </nav>
  20. <main>
  21. <div class="container">
  22. <h2>background color: <span class="color">#000</span></h2>
  23. <button class="btn btn-hero" id="btn">click me</button>
  24. </div>
  25. </main>
  26. <!-- javascript -->
  27. <script src="app.js"></script>
  28. </body>
  29. </html>
展开查看全部
gcxthw6b

gcxthw6b3#

好的,你要做的是点击按钮来显示数组中的下一种颜色吗?如果是这样,那么这就是javascript

  1. const colors = ["green", "red", "rgba(133,122,200)", "#f15025"]
  2. const colorIdentifier = document.querySelector(".color")
  3. const button = document.getElementById("btn")
  4. // global index for so we can iterate array 1 by 1
  5. let index = 0
  6. // button on click event listener
  7. button.addEventListener("click", () => {
  8. // set the text of colorIdentifier to colors and use the index of how many times we clicked the button
  9. colorIdentifier.textContent = colors[index]
  10. // iterate the button for on each click this is shorthand for index = index + 1
  11. index++
  12. // check to see if the index is bigger then the length and resets it back to 0
  13. if (index >= colors.length) index = 0
  14. })
展开查看全部

相关问题