使用javascript在2个按钮之间添加水平空间

Adding horizontal space between 2 buttons using javascript

本文关键字:添加 水平 空间 之间 按钮 javascript 2个 使用      更新时间:2023-09-26

我正在使用javascript动态创建提交按钮。我想知道如何在这个过程中在每个其他按钮之间引入水平间距。

我的代码是

var allFields = commonButtons.concat(extraFields);
for (var i = 0; i < allFields.length; i++) {
    var node = document.createElement("input");
    var btName = document.getElementById("submit-buttons");
    node.setAttribute('data-value', allFields[i]);
    node.setAttribute('class', "submitButton");
    node.setAttribute('type', "submit");
    node.setAttribute('id', "test");
    node.setAttribute('name', "");
    node.setAttribute('value', allFields[i]);
    var parent = document.getElementById('submit-buttons');
    parent.insertBefore(node, parent.childNodes[parent.childNodes.length - 2]);
}

我该怎么做?

使用CSS而不是Javascript。无论如何,您正在为所有按钮添加一个类。使用以下代码:

.submitButton {
    margin: 0 10px;
}

您可以尝试使用以下行:

node.style.marginTop='0px 10px';

它将为您的控制添加样式。