css 页面不响应用户输入

4ioopgfo  于 2024-01-09  发布在  其他
关注(0)|答案(2)|浏览(119)

我刚刚开始使用JavaScript和HTML,我的第一个任务是创建一个简短的琐事页面。我的任务是创建一个多项选择测验,如果这是正确的答案选择,按钮就会变成绿色,否则就会变成红色。对于我的问题,正确的答案选择是2。如果我按下正确的答案选择或错误的答案选择,什么都不会发生。

  1. body {
  2. background-color: #fff;
  3. color: #212529;
  4. font-size: 1rem;
  5. font-weight: 400;
  6. line-height: 1.5;
  7. margin: 0;
  8. text-align: left;
  9. }
  10. .container {
  11. margin-left: auto;
  12. margin-right: auto;
  13. padding-left: 15px;
  14. padding-right: 15px;
  15. }
  16. .header {
  17. background-color: #477bff;
  18. color: #fff;
  19. margin-bottom: 2rem;
  20. padding: 2rem 1rem;
  21. text-align: center;
  22. }
  23. .section {
  24. padding: 0.5rem 2rem 1rem 2rem;
  25. }
  26. .section:hover {
  27. background-color: #f5f5f5;
  28. color: #477bff;
  29. transition: color 10s ease-in-out, background-color 0.15s ease-in-out;
  30. }
  31. h1 {
  32. font-family: 'Montserrat', sans-serif;
  33. font-size: 48px;
  34. }
  35. button, input[type="submit"] {
  36. background-color: #d9edff;
  37. border: 1px solid transparent;
  38. border-radius: 0.25rem;
  39. font-size: 0.95rem;
  40. font-weight: 400;
  41. line-height: 1.5;
  42. padding: 0.375rem 0.75rem;
  43. text-align: center;
  44. transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  45. vertical-align: middle;
  46. }
  47. input[type="text"] {
  48. line-height: 1.8;
  49. width: 25%;
  50. }
  51. input[type="text"]:hover {
  52. background-color: #f5f5f5;
  53. transition: color 2s ease-in-out, background-color 0.15s ease-in-out;
  54. }

个字符
我试图在JavaScript中创建一个检查函数,根据按钮编号评估正确性,但似乎什么也没有发生。

zxlwwiss

zxlwwiss1#

为了让你的代码段工作,你必须在你的数字id前面添加一个字母,因为否则你会在选择它们时遇到CSS相关的问题,请参阅下面Scott Marcus的评论和这个页面:https://benfrain.com/when-and-where-you-can-use-numbers-in-id-and-class-names/

  1. body {
  2. background-color: #fff;
  3. color: #212529;
  4. font-size: 1rem;
  5. font-weight: 400;
  6. line-height: 1.5;
  7. margin: 0;
  8. text-align: left;
  9. }
  10. .container {
  11. margin-left: auto;
  12. margin-right: auto;
  13. padding-left: 15px;
  14. padding-right: 15px;
  15. }
  16. .header {
  17. background-color: #477bff;
  18. color: #fff;
  19. margin-bottom: 2rem;
  20. padding: 2rem 1rem;
  21. text-align: center;
  22. }
  23. .section {
  24. padding: 0.5rem 2rem 1rem 2rem;
  25. }
  26. .section:hover {
  27. background-color: #f5f5f5;
  28. color: #477bff;
  29. transition: color 10s ease-in-out, background-color 0.15s ease-in-out;
  30. }
  31. h1 {
  32. font-family: 'Montserrat', sans-serif;
  33. font-size: 48px;
  34. }
  35. button, input[type="submit"] {
  36. background-color: #d9edff;
  37. border: 1px solid transparent;
  38. border-radius: 0.25rem;
  39. font-size: 0.95rem;
  40. font-weight: 400;
  41. line-height: 1.5;
  42. padding: 0.375rem 0.75rem;
  43. text-align: center;
  44. transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  45. vertical-align: middle;
  46. }
  47. input[type="text"] {
  48. line-height: 1.8;
  49. width: 25%;
  50. }
  51. input[type="text"]:hover {
  52. background-color: #f5f5f5;
  53. transition: color 2s ease-in-out, background-color 0.15s ease-in-out;
  54. }

个字符
除此之外,您应该遵循DRY(不要重复自己)规则并重写代码,以便只有.addEventListener()赋值。

  1. button=document.querySelector(`#${id}`)


check()函数中的赋值也可以替换为:

  1. button=id


在这两种情况下,它前面都应该有一个let,否则会污染全局名称空间。

展开查看全部
idv4meu8

idv4meu82#

您遇到的核心问题是您正在调用尚未在页面上创建的HTML元素。通过降低JavaScript,您可以正确调用HTML元素。
另一个我看到你遇到的问题是你的id是数字。你可以用数字来表示id,但是在id中使用字母可以让你很容易地识别你是在使用字符串还是数字。
这使我能够制作一个可以工作的代码版本(参见下面的代码片段)。

  1. // TODO: Add code to check answers to questions
  2. function check(id) {
  3. // Account for the id's now being strings starting with the word 'button'
  4. button = document.querySelector(`#button${id}`);
  5. if (id == '2') {
  6. button.style.backgroundColor = 'green';
  7. } else {
  8. button.style.backgroundColor = 'red';
  9. }
  10. }
  11. document.getElementById('button1').addEventListener('click', function() {
  12. check('1');
  13. });
  14. document.getElementById('button2').addEventListener('click', function() {
  15. check('2');
  16. });
  17. document.getElementById('button3').addEventListener('click', function() {
  18. check('3');
  19. });
  20. document.getElementById('button4').addEventListener('click', function() {
  21. check('4');
  22. });
  1. body {
  2. background-color: #fff;
  3. color: #212529;
  4. font-size: 1rem;
  5. font-weight: 400;
  6. line-height: 1.5;
  7. margin: 0;
  8. text-align: left;
  9. }
  10. .container {
  11. margin-left: auto;
  12. margin-right: auto;
  13. padding-left: 15px;
  14. padding-right: 15px;
  15. }
  16. .header {
  17. background-color: #477bff;
  18. color: #fff;
  19. margin-bottom: 2rem;
  20. padding: 2rem 1rem;
  21. text-align: center;
  22. }
  23. .section {
  24. padding: 0.5rem 2rem 1rem 2rem;
  25. }
  26. .section:hover {
  27. background-color: #f5f5f5;
  28. color: #477bff;
  29. transition: color 10s ease-in-out, background-color 0.15s ease-in-out;
  30. }
  31. h1 {
  32. font-family: 'Montserrat', sans-serif;
  33. font-size: 48px;
  34. }
  35. button,
  36. input[type="submit"] {
  37. background-color: #d9edff;
  38. border: 1px solid transparent;
  39. border-radius: 0.25rem;
  40. font-size: 0.95rem;
  41. font-weight: 400;
  42. line-height: 1.5;
  43. padding: 0.375rem 0.75rem;
  44. text-align: center;
  45. transition: color 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
  46. vertical-align: middle;
  47. }
  48. input[type="text"] {
  49. line-height: 1.8;
  50. width: 25%;
  51. }
  52. input[type="text"]:hover {
  53. background-color: #f5f5f5;
  54. transition: color 2s ease-in-out, background-color 0.15s ease-in-out;
  55. }
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap" rel="stylesheet">
  5. <link href="styles.css" rel="stylesheet">
  6. <title>Trivia!</title>
  7. </head>
  8. <body>
  9. <div class="header">
  10. <h1>Trivia!</h1>
  11. </div>
  12. <div class="container">
  13. <div class="section">
  14. <h2> Part 1: Multiple Choice </h2>
  15. <hr>
  16. <!-- TODO: Add multiple choice question here -->
  17. <h3>
  18. What does the Mushroom tell the other Mushroom when they're the only Mushrooms in their world?
  19. </h3>
  20. <!-- Add letters into your id's, this way your id's won't be confused for numbers when they are strings -->
  21. <button id="button1"> I'm a Fun Guy!</button>
  22. <button id="button2">There's 2 mushroom?</button>
  23. <button id="button3"> Let me trufle over to you.</button>
  24. <button id="button4"> "Sigh". Lets wallop on the pizza-ria...</button>
  25. </div>
  26. <div class="section">
  27. <h2>Part 2: Free Response</h2>
  28. <hr>
  29. <!-- TODO: Add free response question here -->
  30. </div>
  31. </div>
  32. <!-- Add your JavaScript here instead -->
  33. <script>
  34. </script>
  35. </body>
  36. </html>
展开查看全部

相关问题