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

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

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"> <!-- 这是问题中的按钮 -->
        <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;
}

JS文件中的代码

/* 使按钮可点击,播放按钮消失,得分线出现 */
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

添加defer属性:

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

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

发表评论

匿名网友

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

确定