显示按钮/容器当条件满足DOM操作时。

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

How to display a button/container when condition is met DOM manipulation

问题

You can make the reset button and the .winSection display when the condition count === 5 is met in the game() function by making the following changes:

  1. To make the reset button display, replace this line:
resetBtn.classList.add = 'fadeIn';

with this line:

resetBtn.classList.add('fadeIn');
  1. To display the .winSection, add this line before setting the resetBtn class:
winSection.style.opacity = '100%';

Here's the updated code snippet:

if (count === 5) {
    // Disable R.P.S buttons and make 'reset' button clickable. Present winner and add trophy img
    rpsBtns.style.display = 'none';
    displayHolder.style.display = 'none';

    if (pScore > cScore && pScore > tie) {
        // Add .winSection to display winner
        theWinner.textContent = "player";
    } else if (cScore > pScore && cScore > tie) {
        // Add .winSection to display winner
        theWinner.textContent = "computer";
    } else {
        // Add .winSection to display winner
        winSection.style.opacity = '100%';
    }

    // Add reset Button
    resetBtn.classList.add('fadeIn');
}

This should help you display the reset button and .winSection when the count reaches 5 in your Rock, Paper, Scissors game.

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

pScore = 0; 
cScore = 0;
tie = 0; 

//UI. When play is clicked make R.P.S buttons appear
const startGame= () =&gt; {
    intro = document.querySelector(&#39;.intro&#39;);
    playBtn = document.querySelector(&quot;.playBtn&quot;);
    match = document.querySelector(&#39;.match&#39;);

    playBtn.addEventListener(&#39;click&#39;, e =&gt; {
    match.classList.add(&quot;fadeIn&quot;);
    playBtn.remove();
    });
}


//When &quot;rock&quot;, &quot;paper&quot;, or &quot;scissors&quot; is clicked generate random
//option for computer selection then compare to see who wins
const playGame= () =&gt; {
    button = document.querySelectorAll(&#39;.buttons button&#39;);
    choices= [&quot;rock&quot;, &quot;paper&quot;, &quot;scissors&quot;];
    choiceDisplay = document.querySelector(&#39;.matchContainer&#39;);
    playerDisplay = document.querySelector(&#39;.playerDisplay&#39;);

    button.forEach(button =&gt; {
        button.addEventListener(&#39;click&#39;, function(e) {

            //generate random computerSelection
            computerSelection = choices[ Math.floor((Math.random() *choices.length))];
            
            //display computer and player selection on UI
            playerSelection = this.textContent;
            playerDisplay.textContent = &quot; &quot;+ ` Player: ${playerSelection}`;
            choiceDisplay.textContent = ` Computer: ${computerSelection } ` + &quot; &quot;;         
            
            compareSelection(playerSelection, computerSelection);
        });
    }); 
}    

const compareSelection = ( playerSelection, computerSelection) =&gt; {
    //compare player and computer choice
    if (playerSelection === computerSelection){ console.log(&quot;tie&quot;);
    tie++;
    game();
    return;
    } else if ( playerSelection === &quot;rock&quot; &amp;&amp; computerSelection === &quot;scissors&quot; ||
    playerSelection === &quot;scissors&quot; &amp;&amp; computerSelection === &quot;paper&quot; ||
    playerSelection === &quot;paper&quot; &amp;&amp; computerSelection === &quot;rock&quot; ){ 
    console.log  (`You win! ${playerSelection} beats ${computerSelection}`);
    pScore++;
    game();
    return;  
    } else {
    console.log(`You lose! ${computerSelection} beats ${playerSelection}`); 
    cScore++;
    game();
    return; 
    }
    
}   
const game = () =&gt; {

    playerScore = document.querySelector(&#39;.playerScore&#39;);
    computerScore = document.querySelector(&#39;.computerScore&#39;);
    tieScore = document.querySelector(&#39;.tieScore&#39;);
    resetBtn = document.querySelector(&#39;.resetBtn&#39;);
    rpsBtns = document.querySelector(&#39;.buttons&#39;);
    displayHolder = document.querySelector(&#39;.displayHolder&#39;);
    theWinner = document.querySelector(&#39;.theWinner&#39;);
    winSection = document.querySelector(&#39;.winSection&#39;);
    resetContainer = document.querySelector(&#39;.resetContainer&#39;);

    playerScore.textContent = pScore; 
    computerScore.textContent = cScore;
    tieScore.textContent = tie; 

    count= pScore +cScore +tie; 

    
    if(count === 5){
        //disable R.P.S buttons and make &#39;reset&#39; button clickable. Present winner add trophy img
        rpsBtns.style.display = &#39;none&#39;;
        displayHolder.style.display = &#39;none&#39;;
        
        
        if( pScore &gt; cScore &amp;&amp; pScore &gt; tie ){
            //add .winSection to display winner
            theWinner.textContent = (&quot;player&quot;);
        }
        else if( cScore &gt; pScore &amp;&amp; cScore &gt; tie){
            //add .winSection to display winner
            theWinner.textContent = (&quot;computer&quot;);
        }
        else {
            //add .winSection to display winner
            winSection.style.display = &#39;none&#39;;
            
        } 
        //Add reset Button
        resetBtn.classList.add = &#39;fadeIn&#39;;
    
    }

}

startGame();
playGame();

<!-- language: lang-css -->

body{
    background-color: #844141;
}

.intro, .buttons{
    text-align: center;

}
div.fadeOut{
    opacity: 0;
    
}

div.fadeIn, .resetBtn.fadeIn{
    opacity: 3;
}

.match, .resetBtn{
    opacity: 0%;
    transition: .5s ease;

}

.score-list, .score-list2, .winSection, .resetContainer{
    font-size: 20px;        
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 20px;

}
.computerTitle, .playerTitle, .tieTitle, .winnerTitle{
    margin: 10px;
    font-size: 30px;
}

.winnerTitle{
    font-size: 40px;
}

.playBtn, .resetBtn{
    font-size: 70px;
    font-family: &quot;Time New Roman&quot;, times, serif;
    background-color: #4c586b;
    border-radius: 5px;
    padding: 12px; 
    transition-duration: 1s; 
    
}


/* .reset{
    font-size: 70px;
    font-family: &quot;Time New Roman&quot;, times, serif;
    background-color: #4c586b;
    border-radius: 5px;
    padding: 12px; 
    transition-duration: 1s; 
    opacity: 10;

} */
.matchContainer, .playerDisplay{
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 15px
    
}

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

.rockBtn, .paperBtn, .scissorsBtn{
    font-size: 50px;
    background-color:#555d5f;
    margin: 15px;
    border-radius: 5px;
    font-family: &quot;Time New Roman&quot;, times, serif;
    
}

.rockBtn:hover, .paperBtn:hover, .scissorsBtn:hover, .playBtn:hover, .resetBtn:hover{
    background-color: #3c3e3f;
    transition: .7s;
}

.title{
    font-size: 45px;
}
.titleStatement{
    font-size: 30px;
}

span{
    font-size: 25px;
}

.theWinner{
    font-size: 53px;
    margin: 13px; 
}
.winSection{
    opacity: 0%;
    transition: 1s;
}


img{
    margin: 10px;
    width: 45px;
    height: 50px;
    
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Rock Paper Scissors&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
    &lt;script src=&quot;rps.js&quot; defer=&quot;defer&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;container&quot;&gt;
    &lt;div class=&quot;intro&quot;&gt;
        &lt;h1 class=&quot;title&quot;&gt;Rock Paper Scissors&lt;/h1&gt;
        &lt;h2 class=&quot;titleStatement&quot;&gt;Challenge the computer five rounds of R.P.S to see who wins!&lt;/h2&gt;
        &lt;!--once this is pressed the screen will transition to everything below --&gt;
        &lt;button class=&quot;playBtn&quot;&gt;Play!!&lt;/button&gt; 
    &lt;/div&gt;  

    &lt;div class=&quot;match&quot;&gt;
          
        &lt;div class=&quot;buttons&quot;&gt;
            &lt;button class=&quot;rockBtn&quot;&gt;rock&lt;/button&gt;
            &lt;button class=&quot;paperBtn&quot;&gt;paper&lt;/button&gt;
            &lt;button class=&quot;scissorsBtn&quot;&gt;scissors&lt;/button&gt;
        &lt;/div&gt;

        &lt;div class=&quot;displayHolder&quot;&gt;
            &lt;div class=&quot;playerDisplay&quot;&gt;&lt;/div&gt;
            &lt;div class=&quot;matchContainer&quot;&gt;&lt;/div&gt;
            
        &lt;/div&gt;
        &lt;div class=&quot;score-list&quot;&gt;
            &lt;p class=&quot;playerTitle&quot;&gt;Player Score: &lt;/p&gt;
                &lt;span class=&quot;playerScore&quot;&gt;0&lt;/span&gt;
            &lt;p class=&quot;computerTitle&quot;&gt;Computer Score:&lt;/p&gt;
                &lt;span class=&quot;computerScore&quot;&gt;0&lt;/span&gt;
            
        &lt;/div&gt;    
            &lt;div class=&quot;score-list2&quot;&gt;
                &lt;p class=&quot;tieTitle&quot;&gt;Tie: &lt;/p&gt;
                &lt;span class=&quot;tieScore&quot;&gt;0&lt;/span&gt;
            &lt;/div&gt;
        
        &lt;div class=&quot;winSection&quot;&gt;
        &lt;h3 class=&quot;winnerTitle&quot;&gt;Winner: &lt;/h3&gt;
            &lt;span class=&quot;theWinner&quot;&gt;?&lt;/span&gt;
            &lt;img src=&quot;/trophy.png&quot; alt=&quot;trophy image&quot;&gt;
        &lt;/div&gt;
        &lt;div class=&quot;resetContainer&quot;&gt;
            &lt;button class=&quot;resetBtn&quot;&gt;Play Again!&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

I am finishing the touches on my R.P.S project and I can't find the right style to get my resetBtn to display in the function game() when the condition "count===5" is met. I would also like some help displaying the .winSection when the conditions inside count === 5 is met. I don't know how i did the .intro section and made the play button disappear and the reset appear after lol

> I tried adding the reset button like how I did the first part (after clicking playbtn) using resetBtn.classList.add = 'fadeIn'; but that didn't work. I messed with the opacity in the css maybe I am crossing over some of the same code in my .css file. After spending some time on this I realize I should just ask for help. Thank you in advance for helping!

答案1

得分: 0

以下是代码中需要注意的问题和建议的翻译:

  • 添加类的JS函数是 element.classList.add('my-class'),而不是 .add = "my-class"。这样可以解决按钮的问题。
  • 你需要为按钮注册一个事件,否则游戏不会重新开始。
  • 不要忘记 opacity 是一个介于 0 到 1 或 0% 到 100% 之间的值,你设置了 opacity: 3
  • 默认按钮类型是 submit,但你没有提交任何内容,所以将按钮类型设置为 button,例如 <button type="button">
  • 缩进混乱(但我猜这是由于复制粘贴引起的)。
  • 不要使用 .remove() 移除 playBtn,我猜你会在重新开始游戏时需要它,或者在点击重新开始按钮后直接切换到游戏时可以移除。

希望这些建议对你有帮助。

英文:

the js function to add a class is element.classList.add(&#39;my-class&#39;) not .add = &quot;my-class&quot;. that solves the button issue. you have to register an event to that button otherwise the game won't restart.

other things:

  • opacity is a value between 0 and 1 or 0% and 100% (you have opacity: 3)
  • the default button type is submit but you don't submit anything, so set the type to button &lt;button type=&quot;button&quot;&gt;
  • indentation is a mess (but I guess that's due to copy paste)
  • don't .remove() your playBtn, I guess you will need it for restarting? or in case you directly switch to the game after clicking the restart button then u can do it ofc.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

pScore = 0; 
cScore = 0;
tie = 0; 

//UI. When play is clicked make R.P.S buttons appear
const startGame= () =&gt; {
    intro = document.querySelector(&#39;.intro&#39;);
    playBtn = document.querySelector(&quot;.playBtn&quot;);
    match = document.querySelector(&#39;.match&#39;);

    playBtn.addEventListener(&#39;click&#39;, e =&gt; {
    match.classList.add(&quot;fadeIn&quot;);
    playBtn.style.display = &#39;none&#39;;
    });
}


//When &quot;rock&quot;, &quot;paper&quot;, or &quot;scissors&quot; is clicked generate random
//option for computer selection then compare to see who wins
const playGame= () =&gt; {
    button = document.querySelectorAll(&#39;.buttons button&#39;);
    choices= [&quot;rock&quot;, &quot;paper&quot;, &quot;scissors&quot;];
    choiceDisplay = document.querySelector(&#39;.matchContainer&#39;);
    playerDisplay = document.querySelector(&#39;.playerDisplay&#39;);

    button.forEach(button =&gt; {
        button.addEventListener(&#39;click&#39;, function(e) {

            //generate random computerSelection
            computerSelection = choices[ Math.floor((Math.random() *choices.length))];
            
            //display computer and player selection on UI
            playerSelection = this.textContent;
            playerDisplay.textContent = &quot; &quot;+ ` Player: ${playerSelection}`;
            choiceDisplay.textContent = ` Computer: ${computerSelection } ` + &quot; &quot;;         
            
            compareSelection(playerSelection, computerSelection);
        });
    }); 
}    

const compareSelection = ( playerSelection, computerSelection) =&gt; {
    //compare player and computer choice
    if (playerSelection === computerSelection){ console.log(&quot;tie&quot;);
    tie++;
    game();
    return;
    } else if ( playerSelection === &quot;rock&quot; &amp;&amp; computerSelection === &quot;scissors&quot; ||
    playerSelection === &quot;scissors&quot; &amp;&amp; computerSelection === &quot;paper&quot; ||
    playerSelection === &quot;paper&quot; &amp;&amp; computerSelection === &quot;rock&quot; ){ 
    console.log  (`You win! ${playerSelection} beats ${computerSelection}`);
    pScore++;
    game();
    return;  
    } else {
    console.log(`You lose! ${computerSelection} beats ${playerSelection}`); 
    cScore++;
    game();
    return; 
    }
    
}   
const game = () =&gt; {

    playerScore = document.querySelector(&#39;.playerScore&#39;);
    computerScore = document.querySelector(&#39;.computerScore&#39;);
    tieScore = document.querySelector(&#39;.tieScore&#39;);
    resetBtn = document.querySelector(&#39;.resetBtn&#39;);
    rpsBtns = document.querySelector(&#39;.buttons&#39;);
    displayHolder = document.querySelector(&#39;.displayHolder&#39;);
    theWinner = document.querySelector(&#39;.theWinner&#39;);
    winSection = document.querySelector(&#39;.winSection&#39;);
    resetContainer = document.querySelector(&#39;.resetContainer&#39;);

    playerScore.textContent = pScore; 
    computerScore.textContent = cScore;
    tieScore.textContent = tie; 

    count= pScore +cScore +tie; 

    
    if(count === 5){
        //disable R.P.S buttons and make &#39;reset&#39; button clickable. Present winner add trophy img
        rpsBtns.style.display = &#39;none&#39;;
        displayHolder.style.display = &#39;none&#39;;
        
        
        if( pScore &gt; cScore &amp;&amp; pScore &gt; tie ){
            //add .winSection to display winner
            theWinner.textContent = (&quot;player&quot;);
        }
        else if( cScore &gt; pScore &amp;&amp; cScore &gt; tie){
            //add .winSection to display winner
            theWinner.textContent = (&quot;computer&quot;);
        }
        else {
            //add .winSection to display winner
            winSection.style.display = &#39;none&#39;;
            
        } 
        //Add reset Button
        resetBtn.classList.add(&#39;fadeIn&#39;);
        // click handler missing to restart game
    }

}

startGame();
playGame();

<!-- language: lang-css -->

body{
    background-color: #844141;
}

.intro, .buttons{
    text-align: center;

}
div.fadeOut{
    opacity: 0;
    
}

div.fadeIn, .resetBtn.fadeIn{
    opacity: 1;
}

.match, .resetBtn{
    opacity: 0%;
    transition: .5s ease;

}

.score-list, .score-list2, .winSection, .resetContainer{
    font-size: 20px;        
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 20px;

}
.computerTitle, .playerTitle, .tieTitle, .winnerTitle{
    margin: 10px;
    font-size: 30px;
}

.winnerTitle{
    font-size: 40px;
}

.playBtn, .resetBtn{
    font-size: 70px;
    font-family: &quot;Time New Roman&quot;, times, serif;
    background-color: #4c586b;
    border-radius: 5px;
    padding: 12px; 
    transition-duration: 1s; 
    
}


/* .reset{
    font-size: 70px;
    font-family: &quot;Time New Roman&quot;, times, serif;
    background-color: #4c586b;
    border-radius: 5px;
    padding: 12px; 
    transition-duration: 1s; 
    opacity: 10;

} */
.matchContainer, .playerDisplay{
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 15px
    
}

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

.rockBtn, .paperBtn, .scissorsBtn{
    font-size: 50px;
    background-color:#555d5f;
    margin: 15px;
    border-radius: 5px;
    font-family: &quot;Time New Roman&quot;, times, serif;
    
}

.rockBtn:hover, .paperBtn:hover, .scissorsBtn:hover, .playBtn:hover, .resetBtn:hover{
    background-color: #3c3e3f;
    transition: .7s;
}

.title{
    font-size: 45px;
}
.titleStatement{
    font-size: 30px;
}

span{
    font-size: 25px;
}

.theWinner{
    font-size: 53px;
    margin: 13px; 
}
.winSection{
    opacity: 0%;
    transition: 1s;
}


img{
    margin: 10px;
    width: 45px;
    height: 50px;
    
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Rock Paper Scissors&lt;/title&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot;&gt;
    &lt;script src=&quot;rps.js&quot; defer=&quot;defer&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;container&quot;&gt;
    &lt;div class=&quot;intro&quot;&gt;
        &lt;h1 class=&quot;title&quot;&gt;Rock Paper Scissors&lt;/h1&gt;
        &lt;h2 class=&quot;titleStatement&quot;&gt;Challenge the computer five rounds of R.P.S to see who wins!&lt;/h2&gt;
        &lt;!--once this is pressed the screen will transition to everything below --&gt;
        &lt;button type=&quot;button&quot; class=&quot;playBtn&quot;&gt;Play!!&lt;/button&gt; 
    &lt;/div&gt;  

    &lt;div class=&quot;match&quot;&gt;
          
        &lt;div class=&quot;buttons&quot;&gt;
            &lt;button class=&quot;rockBtn&quot;&gt;rock&lt;/button&gt;
            &lt;button class=&quot;paperBtn&quot;&gt;paper&lt;/button&gt;
            &lt;button class=&quot;scissorsBtn&quot;&gt;scissors&lt;/button&gt;
        &lt;/div&gt;

        &lt;div class=&quot;displayHolder&quot;&gt;
            &lt;div class=&quot;playerDisplay&quot;&gt;&lt;/div&gt;
            &lt;div class=&quot;matchContainer&quot;&gt;&lt;/div&gt;
        &lt;/div&gt;
        &lt;div class=&quot;score-list&quot;&gt;
            &lt;p class=&quot;playerTitle&quot;&gt;Player Score: &lt;/p&gt;
                &lt;span class=&quot;playerScore&quot;&gt;0&lt;/span&gt;
            &lt;p class=&quot;computerTitle&quot;&gt;Computer Score:&lt;/p&gt;
                &lt;span class=&quot;computerScore&quot;&gt;0&lt;/span&gt;
            
        &lt;/div&gt;    
            &lt;div class=&quot;score-list2&quot;&gt;
                &lt;p class=&quot;tieTitle&quot;&gt;Tie: &lt;/p&gt;
                &lt;span class=&quot;tieScore&quot;&gt;0&lt;/span&gt;
            &lt;/div&gt;
        
        &lt;div class=&quot;winSection&quot;&gt;
        &lt;h3 class=&quot;winnerTitle&quot;&gt;Winner: &lt;/h3&gt;
            &lt;span class=&quot;theWinner&quot;&gt;?&lt;/span&gt;
            &lt;img src=&quot;/trophy.png&quot; alt=&quot;trophy image&quot;&gt;
        &lt;/div&gt;
        &lt;div class=&quot;resetContainer&quot;&gt;
            &lt;button type=&quot;button&quot; class=&quot;resetBtn&quot;&gt;Play Again!&lt;/button&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 05:34:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75923949.html
匿名

发表评论

匿名网友

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

确定