简体   繁体   English

如何使用Javascript替换HTML中的CSS边框?

[英]How can I replace a CSS border in HTML using Javascript?

I'm following a simple web development tutorial and would like to have an interactive border depending on the outcome of Rock, Paper, Scissors. 我正在遵循一个简单的Web开发教程,并希望根据Rock,Paper和Scissors的结果提供一个交互式边框。 How can I replace (or overlay) the current CSS border of the Rock, Paper, or Scissor images? 如何替换(或覆盖)Rock,Paper或Scissor图像的当前CSS边框?

According to the tutorial the solution is as shown in the code. 根据教程,解决方案如代码中所示。 However, this is the result that I am getting: https://ibb.co/wBFG2cQ (Sorry, I'm not allowed to embed images directly). 但是,这是我得到的结果: https : //ibb.co/wBFG2cQ (对不起,我不允许直接嵌入图像)。 Here is the intended outcome: https://ibb.co/tB7HvWX . 这是预期的结果: https : //ibb.co/tB7HvWX Lastly, a link to the website hosted on a free hosting site where all source code is viewable https://rockpaperscissors-ndsamu.netlify.com/ . 最后,指向在免费托管站点上托管的网站的链接,在该站点上可以查看所有源代码https://rockpaperscissors-ndsamu.netlify.com/

JavaScript Code (last 2 lines): JavaScript代码(最后2行):

function win(userChoice, computerChoice) {
    userScore++;
    userScore_span.innerHTML = userScore;
    result_p.innerHTML = `${convertToWord(userChoice)} beats ${convertToWord(computerChoice)}. You win!`;
    document.getElementById('crown').style.left="-100px";
    document.getElementById('crown').style.display="block";
    document.getElementById(userChoice).classList.add('green-glow');
    setTimeout(function() { document.getElementById(userChoice).classList.remove('green-glow') }, 1000);
}

CSS Code (for each image): CSS代码(针对每个图像):

.choice {
    border: 4px solid #25272E;
    border-radius: 50%;
    margin: 0 20px;
    padding: 15px;
    display: inline-block;
    transition: all 0.3s ease;
}

CSS Code (for Winning): CSS代码(获奖):

.green-glow {
    border: 4px solid #4DCC7D;
    border-radius: 50%;
    box-shadow: 0 0 10px #3DA364;
}

Instead, I would like the green circular border to replace or overlay the current border rather than appearing within it. 相反,我希望绿色圆形边框替换或覆盖当前边框,而不是出现在其中。

Thank you in advance! 先感谢您!

Without seeing your full code, It's hard to tell what's causing that. 没有看到完整的代码,很难说出是什么原因造成的。 My best guess is padding: 15px is the culprit. 我最好的猜测是padding: 15px是元凶。 If it is, then Maybe try adding * { box-sizing: border-box; } 如果是,那么也许尝试添加* { box-sizing: border-box; } * { box-sizing: border-box; } to your CSS and see if that fixes things. * { box-sizing: border-box; }插入CSS,看看是否可以解决问题。

Here's some code that shows how to do what they are doing, it's been simplified. 这是一些代码,显示了如何做他们正在做的事情,它已被简化。 You can activate a circle by clicking on it. 您可以通过单击激活一个圆圈。

 function win(userChoice, computerChoice) { document.getElementById(userChoice).classList.add("green-glow"); setTimeout(function() { document.getElementById(userChoice).classList.remove("green-glow"); }, 1000); } // seimple way to pick a random element document.getElementById("choice1").addEventListener("click", function() { win("choice1", ""); }); document.getElementById("choice2").addEventListener("click", function() { win("choice2", ""); }); document.getElementById("choice3").addEventListener("click", function() { win("choice3", ""); }); 
 .row { display: block; } .circle { position: relative; display: inline-block; width: 50px; height: 50px; border: 4px solid #25272e; border-radius: 50%; margin: 0 20px; padding: 0; transition: all 0.3s ease; } .circle.green-glow { border: 4px solid #4dcc7d; border-radius: 50%; box-shadow: 0 0 10px #3da364; } 
 <div class="row"> <div id="choice1" class="circle"> </div> <div id="choice2" class="circle"> </div> <div id="choice3" class="circle"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM