gpt4 book ai didi

javascript - 我怎样才能渲染一个红色的盒子?

转载 作者:行者123 更新时间:2023-11-30 09:12:39 25 4
gpt4 key购买 nike

我想在我的屏幕上渲染一个红色框,点击“Criar quadrado”按钮。我的代码可以做到,但只能做到一次。我想在每次点击时呈现一个红色框。那是我的代码:

//Crie um botão que ao ser clicado cria um novo elemento em tela com a forma de um quadrado
//vermelho com 100px de altura e largura. Sempre que o botão for clicado um novo quadrado deve
//aparecer na tela.

function criarQuadrado() {
var boxElement = document.querySelector(".box");

boxElement.style.width = 300;
boxElement.style.height = 300;
boxElement.style.backgroundColor = "#f00";
}
<div class="box"></div>
<button onclick="criarQuadrado()">Criar quadrado</button>

它呈现,但我想在每次单击时并排放置另一个框。我该怎么做?

最佳答案

      function criarQuadrado() {
var boxElement = document.createElement("div")

boxElement.style.width = "30px";
boxElement.style.height = "30px";
boxElement.style.display = "inline-block";
boxElement.style.backgroundColor = "#f00";

var container = document.getElementById("container")
container.appendChild(boxElement)
}
<div id="container"></div>
<button type="button" onclick="criarQuadrado()">Click me</button>

关于javascript - 我怎样才能渲染一个红色的盒子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57398231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com