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

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

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

问题

一些背景信息,我正在为一个剪刀石头布网页的用户界面进行开发。点击播放按钮后,按钮会消失,然后出现一个记分牌和代表三种动作的图标,这些图标可以点击。然而,目前的问题是,当我点击按钮时,没有任何东西出现。以下是相关的代码:

HTML代码

<head>
    <link rel="stylesheet"  type="text/css" href="static/css/index.css">
    <script src="index.js"></script>
</head>
<body> 
    <div class="title">
        <div class ="main">
            <h1>Rock Paper Scissors</h1>
        </div>
        <div>
            <h2>First one to three</h2>
        </div>
        
    </div>
    <div class="button-layout">
        <a href="#" id="rock" class="button disabled">
            <i class="fa-solid fa-hand-back-fist"></i>
            <caption>Rock</caption>
        </a>
        <a href="#" id="paper" class="button disabled">
            <i class="fa-solid fa-hand"></i>
            <caption>Paper</caption>
        </a>
        <a href="#" id="scissors" class="button disabled">
            <i class="fa-solid fa-hand-scissors"></i>
            <caption>Scissors</caption>
        </a>
    </div>
    <div id="play-button"> <!--The button in question-->
        <button>Play game</button>
    </div>

    <div class="scoreline hidden">
        <table>
            <tr>
                <th>Human</th>
                <th>Machine</th>
            </tr>
            <tr class="score">
                <td>0</td>
                <td>0</td>
            </tr>
        </table>
    </div>
</body>

CSS代码

.button-layout{
    display: flex;
    margin: 10px;
    background-color: whitesmoke;
    justify-content: space-around;
}

.button{
    display: flex;
    flex-direction: column;
    padding: 10px;
    align-items: center;
    text-decoration: none;
}

i{
    font-size: 60px;
    margin-bottom: 10px;
}

#play-button{
    display: flex;
    justify-content: center;
}

.replay-button{
    display: flex;
    justify-content: center;
}

table, th, td{
    width:40%;
    border: 1px solid #ddd;
    border-collapse: collapse;
}

.scoreline{
    display: flex;
    justify-content: center;
}

td{
    text-align: center;
}

.disabled{
    pointer-events: none;
}

.hidden{
    display: none;
}

JavaScript文件中的代码

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

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

&lt;head&gt;
&lt;link rel=&quot;stylesheet&quot;  type=&quot;text/css&quot; href=&quot;static/css/index.css&quot;&gt;
&lt;script src=&quot;index.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
body&gt; 
&lt;div class=&quot;title&quot;&gt;
&lt;div class =&quot;main&quot;&gt;
&lt;h1&gt;Rock Paper Scissors&lt;/h1&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;h2&gt;First one to three&lt;/h2&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;button-layout&quot;&gt;
&lt;a href=&quot;#&quot; id=&quot;rock&quot; class=&quot;button disabled&quot;&gt;
&lt;i class=&quot;fa-solid fa-hand-back-fist&quot;&gt;&lt;/i&gt;
&lt;caption&gt;Rock&lt;/caption&gt;
&lt;/a&gt;
&lt;a href=&quot;#&quot; id=&quot;paper&quot; class=&quot;button disabled&quot;&gt;
&lt;i class=&quot;fa-solid fa-hand&quot;&gt;&lt;/i&gt;
&lt;caption&gt;Paper&lt;/caption&gt;
&lt;/a&gt;
&lt;a href=&quot;#&quot; id=&quot;scissors&quot; class=&quot;button disabled&quot;&gt;
&lt;i class=&quot;fa-solid fa-hand-scissors&quot;&gt;&lt;/i&gt;
&lt;caption&gt;Scissors&lt;/caption&gt;
&lt;/a&gt;
&lt;/div&gt;
&lt;div id=&quot;play-button&quot;&gt; &lt;!--The button in question--&gt;
&lt;button&gt;Play game&lt;/button&gt;
&lt;/div&gt;
&lt;div class=&quot;scoreline hidden&quot;&gt;
&lt;table&gt;
&lt;tr&gt;
&lt;th&gt;Human&lt;/th&gt;
&lt;th&gt;Machine&lt;/th&gt;
&lt;/tr&gt;
&lt;tr class=&quot;score&quot;&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/body&gt;

Code in CSS

.button-layout{
display: flex;
margin: 10px;
background-color: whitesmoke;
justify-content: space-around;
}
.button{
display: flex;
flex-direction: column;
padding: 10px;
align-items: center;
text-decoration: none;
}
i{
font-size: 60px;
margin-bottom: 10px;
}
#play-button{
display: flex;
justify-content: center;
}
.replay-button{
display: flex;
justify-content: center;
}
table, th, td{
width:40%;
border: 1px solid #ddd;
border-collapse: collapse;
}
.scoreline{
display: flex;
justify-content: center;
}
td{
text-align: center;
}
.disabled{
pointer-events: none;
}
.hidden{
display: none;
}

Code in my JS file

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

答案1

得分: 0

好的,以下是翻译好的内容:

好的,你的JavaScript代码无法运行,因为它没有正确链接。请添加defer属性:

英文:

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

&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.html
匿名

发表评论

匿名网友

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

确定