gpt4 book ai didi

JavaScript 在循环中创建按钮

转载 作者:行者123 更新时间:2023-11-28 03:10:00 25 4
gpt4 key购买 nike

我想在每个按钮后中断并居中,有什么建议吗? setAttribute 不起作用并且不添加中断

for (var j = 0; j <= 6; j++) {
var btn = document.createElement("BUTTON");
var t = document.createTextNode(sm[j] + " " + sy[j]);
btn.appendChild(t);
document.body.appendChild(btn);
}

最佳答案

jsfiddle

HTML

<div id='theParent' class='center_the_stuff'>

</div>

JS

function addInput(type, value, name, id, onclick, parentId) {
//Create an input type dynamically.
var element = document.createElement("input");
//Assign different attributes to the element.
element.type = type;
element.value = value; // Really? You want the default value to be the type string?
element.name = name; // And the name too?
element.id = id;
element.onclick = onclick;

var parent = document.getElementById(parentId);
//Append the element in page (in span).
parent.appendChild(element);
}

function addBreak(parentId) {
var br = document.createElement("br");
var parent = document.getElementById(parentId);
parent.appendChild(br);
}

window.onload = function () {
for (var j = 0; j <= 6; j++) {
var temp = 'mybutton' + j;
addInput('button', temp, temp, temp, undefined, 'theParent');
addBreak('theParent');
}
}

CSS

.center_the_stuff {
text-align: center;
}

关于JavaScript 在循环中创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30940914/

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