我的按钮不遵循JS脚本,尽管我已经链接了文件。

huangapple go评论103阅读模式
英文:

My button doesn't follow JS script even though I have linked the file

问题

为了一些背景信息,我正在为一个剪刀石头布网页的用户界面工作。点击播放按钮后,按钮将消失,让出得分线和表示三个动作的图标可点击。然而,当前情况下,由于某种原因,当我点击按钮时,什么都不会出现。以下是相关代码:

HTML中的代码

  1. <head>
  2. <link rel="stylesheet" type="text/css" href="static/css/index.css">
  3. <script src="index.js"></script>
  4. </head>
  5. <body>
  6. <div class="title">
  7. <div class="main">
  8. <h1>Rock Paper Scissors</h1>
  9. </div>
  10. <div>
  11. <h2>First one to three</h2>
  12. </div>
  13. </div>
  14. <div class="button-layout">
  15. <a href="#" id="rock" class="button disabled">
  16. <i class="fa-solid fa-hand-back-fist"></i>
  17. <caption>Rock</caption>
  18. </a>
  19. <a href="#" id="paper" class="button disabled">
  20. <i class="fa-solid fa-hand"></i>
  21. <caption>Paper</caption>
  22. </a>
  23. <a href="#" id="scissors" class="button disabled">
  24. <i class="fa-solid fa-hand-scissors"></i>
  25. <caption>Scissors</caption>
  26. </a>
  27. </div>
  28. <div id="play-button"> <!-- 这是问题中的按钮 -->
  29. <button>Play game</button>
  30. </div>
  31. <div class="scoreline hidden">
  32. <table>
  33. <tr>
  34. <th>Human</th>
  35. <th>Machine</th>
  36. </tr>
  37. <tr class="score">
  38. <td>0</td>
  39. <td>0</td>
  40. </tr>
  41. </table>
  42. </div>
  43. </body>

CSS中的代码

  1. .button-layout {
  2. display: flex;
  3. margin: 10px;
  4. background-color: whitesmoke;
  5. justify-content: space-around;
  6. }
  7. .button {
  8. display: flex;
  9. flex-direction: column;
  10. padding: 10px;
  11. align-items: center;
  12. text-decoration: none;
  13. }
  14. i {
  15. font-size: 60px;
  16. margin-bottom: 10px;
  17. }
  18. #play-button {
  19. display: flex;
  20. justify-content: center;
  21. }
  22. .replay-button {
  23. display: flex;
  24. justify-content: center;
  25. }
  26. table,
  27. th,
  28. td {
  29. width: 40%;
  30. border: 1px solid #ddd;
  31. border-collapse: collapse;
  32. }
  33. .scoreline {
  34. display: flex;
  35. justify-content: center;
  36. }
  37. td {
  38. text-align: center;
  39. }
  40. .disabled {
  41. pointer-events: none;
  42. }
  43. .hidden {
  44. display: none;
  45. }

JS文件中的代码

  1. /* 使按钮可点击,播放按钮消失,得分线出现 */
  2. document.getElementById('play-button').addEventListener("click", function(){
  3. const buttons = document.getElementsByClassName('button')
  4. for (i = 0; i < buttons.length; i++){
  5. buttons[i].classList.remove('disabled')
  6. }
  7. document.getElementById('play-button').style.display = "none"
  8. const scoreline = document.querySelector(".scoreline");
  9. scoreline.classList.remove("hidden")
  10. })
英文:

For a bit of context, I'm working on the UI for a rock paper scissors webpage. Upon clicking the play button, the button would disappear and make way for a scoreline and the icons representing 3 moves above would be made clickable. However, currently, for some reason, when I click the button, nothing would materialize. Here is the revelant code:

Code in html

  1. &lt;head&gt;
  2. &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;static/css/index.css&quot;&gt;
  3. &lt;script src=&quot;index.js&quot;&gt;&lt;/script&gt;
  4. &lt;/head&gt;
  5. body&gt;
  6. &lt;div class=&quot;title&quot;&gt;
  7. &lt;div class =&quot;main&quot;&gt;
  8. &lt;h1&gt;Rock Paper Scissors&lt;/h1&gt;
  9. &lt;/div&gt;
  10. &lt;div&gt;
  11. &lt;h2&gt;First one to three&lt;/h2&gt;
  12. &lt;/div&gt;
  13. &lt;/div&gt;
  14. &lt;div class=&quot;button-layout&quot;&gt;
  15. &lt;a href=&quot;#&quot; id=&quot;rock&quot; class=&quot;button disabled&quot;&gt;
  16. &lt;i class=&quot;fa-solid fa-hand-back-fist&quot;&gt;&lt;/i&gt;
  17. &lt;caption&gt;Rock&lt;/caption&gt;
  18. &lt;/a&gt;
  19. &lt;a href=&quot;#&quot; id=&quot;paper&quot; class=&quot;button disabled&quot;&gt;
  20. &lt;i class=&quot;fa-solid fa-hand&quot;&gt;&lt;/i&gt;
  21. &lt;caption&gt;Paper&lt;/caption&gt;
  22. &lt;/a&gt;
  23. &lt;a href=&quot;#&quot; id=&quot;scissors&quot; class=&quot;button disabled&quot;&gt;
  24. &lt;i class=&quot;fa-solid fa-hand-scissors&quot;&gt;&lt;/i&gt;
  25. &lt;caption&gt;Scissors&lt;/caption&gt;
  26. &lt;/a&gt;
  27. &lt;/div&gt;
  28. &lt;div id=&quot;play-button&quot;&gt; &lt;!--The button in question--&gt;
  29. &lt;button&gt;Play game&lt;/button&gt;
  30. &lt;/div&gt;
  31. &lt;div class=&quot;scoreline hidden&quot;&gt;
  32. &lt;table&gt;
  33. &lt;tr&gt;
  34. &lt;th&gt;Human&lt;/th&gt;
  35. &lt;th&gt;Machine&lt;/th&gt;
  36. &lt;/tr&gt;
  37. &lt;tr class=&quot;score&quot;&gt;
  38. &lt;td&gt;0&lt;/td&gt;
  39. &lt;td&gt;0&lt;/td&gt;
  40. &lt;/tr&gt;
  41. &lt;/table&gt;
  42. &lt;/div&gt;
  43. &lt;/body&gt;

Code in CSS

  1. .button-layout{
  2. display: flex;
  3. margin: 10px;
  4. background-color: whitesmoke;
  5. justify-content: space-around;
  6. }
  7. .button{
  8. display: flex;
  9. flex-direction: column;
  10. padding: 10px;
  11. align-items: center;
  12. text-decoration: none;
  13. }
  14. i{
  15. font-size: 60px;
  16. margin-bottom: 10px;
  17. }
  18. #play-button{
  19. display: flex;
  20. justify-content: center;
  21. }
  22. .replay-button{
  23. display: flex;
  24. justify-content: center;
  25. }
  26. table, th, td{
  27. width:40%;
  28. border: 1px solid #ddd;
  29. border-collapse: collapse;
  30. }
  31. .scoreline{
  32. display: flex;
  33. justify-content: center;
  34. }
  35. td{
  36. text-align: center;
  37. }
  38. .disabled{
  39. pointer-events: none;
  40. }
  41. .hidden{
  42. display: none;
  43. }

Code in my JS file

  1. /*Make the buttons clickable, the play-button disappear and the scoreline to appear*/
  2. document.getElementById(&#39;play-button&#39;). addEventListener(&quot;click&quot;, function(){
  3. const buttons = document.getElementsByClassName(&#39;button&#39;)
  4. for (i=0; i &lt;buttons.length; i++){
  5. buttons[i].classList.remove(&#39;disabled&#39;)
  6. }
  7. document.getElementById(&#39;play-button&#39;).style.display = &quot;none&quot;
  8. const scoreline = document.querySelector(&quot;.scoreline&quot;);
  9. scoreline.classList.remove(&quot;hidden&quot;)
  10. })

答案1

得分: 0

添加defer属性:

  1. <script defer src="index.js"></script>
英文:

ok well, your JavaScript isn't going to run cuz its not properly linked. add the defer attribute

  1. &lt;script defer src=&quot;index.js&quot;&gt;&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年8月9日 10:59:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864299-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定