gpt4 book ai didi

javascript - 如何让按钮到javascript中的下一行

转载 作者:行者123 更新时间:2023-11-30 20:40:32 26 4
gpt4 key购买 nike

我正在制作一个井字游戏,我需要在其中创建按钮,然后向其添加事件。一行有三个按钮,下一行有 3 个按钮,下一行有另外三个按钮。我已经使用了 break 标记以及\r\n 符号,但它仍然不起作用。请帮忙。

 <body>
<div id="displaytable"></div>
<script src='jquery-3.3.1.js'></script>
<script>
var table= [];
var blocks = 9;
var player,boardId;
winningCombinations = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]];


$(document).ready(function(){

buttonId = 0;

for (var index = 0; index < blocks; index++) {
button1 = document.createElement("button");

if((index==2||index==5||index==8)&&(buttonId==3||buttonId==6||buttonId==9)){
button1.innerHTML="\r\n"+"<br>";
}

button1.innerHTML = " + " ;
button1.id = buttonId;
button1.setAttribute("value", buttonId);
button1.setAttribute("text", buttonId);

button1.style.fontFamily = "Times New Roman";
button1.style.backgroundSize = "50px";
button1.style.backgroundColor = "#C0C0C0";
button1.style.fontSize = "25px";
button1.style.marginBottom = "10px";
button1.style.marginLeft = "5px";
button1.style.marginRight = "5px";
document.body.appendChild(button1);
buttonId++;



}
});

</script>

</body>

最佳答案

CSS 解决方案

您可以使用 CSS 来做到这一点,将按钮显示为 block 并将它们 float 到左侧。

每隔三个按钮清除一次。

var table = [];
var blocks = 9;
var player, boardId;
winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];


$(document).ready(function() {

buttonId = 0;

for (var index = 0; index < blocks; index++) {
button1 = document.createElement("button");

if ((index == 2 || index == 5 || index == 8) && (buttonId == 3 || buttonId == 6 || buttonId == 9)) {
button1.innerHTML = "\r\n" + "<br>";
}

button1.innerHTML = " + ";
button1.id = buttonId;
button1.setAttribute("value", buttonId);
button1.setAttribute("text", buttonId);

button1.style.fontFamily = "Times New Roman";
button1.style.backgroundSize = "50px";
button1.style.backgroundColor = "#C0C0C0";
button1.style.fontSize = "25px";
button1.style.marginBottom = "10px";
button1.style.marginLeft = "5px";
button1.style.marginRight = "5px";
document.body.appendChild(button1);
buttonId++;



}
});
 button {
display: block;
float: left;
}
button:nth-of-type(3n+1) {
clear: both;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="displaytable"></div>

JS 解决方案

var table = [];
var blocks = 9;
var player, boardId;
winningCombinations = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
];


$(document).ready(function() {

buttonId = 0;

for (var index = 0; index < blocks; index++) {
button1 = document.createElement("button");

button1.innerHTML = " + ";
button1.id = buttonId;
button1.setAttribute("value", buttonId);
button1.setAttribute("text", buttonId);

button1.style.fontFamily = "Times New Roman";
button1.style.backgroundSize = "50px";
button1.style.backgroundColor = "#C0C0C0";
button1.style.fontSize = "25px";
button1.style.marginBottom = "10px";
button1.style.marginLeft = "5px";
button1.style.marginRight = "5px";
document.body.appendChild(button1);
if( (index+1) % 3 == 0 ) {
document.body.appendChild( document.createElement("br") );
}
buttonId++;



}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="displaytable"></div>

关于javascript - 如何让按钮到javascript中的下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49316935/

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