当我在js脚本中添加带有DOM appendChild()的输入标记时,它们似乎没有边距,因为它们之间没有分离。在html文件中写入的输入标记有这种分隔。元素检验员说他们都有相同的风格。

js脚本:
document.body.appendChild(document.createElement("input"));我不知道如何搜索这个问题,所以我在这里问,发生了什么,如何在js脚本中解决这个问题?
发布于 2020-06-14 13:44:20
添加空白或给类留页边距
// without
document.body.appendChild(document.createElement("input"));
document.body.appendChild(document.createElement("input"));
document.body.appendChild(document.createElement("hr"));
// with whitespace
document.body.appendChild(document.createElement("input"));
document.body.appendChild(document.createTextNode(" "));
document.body.appendChild(document.createElement("input"));
document.body.appendChild(document.createElement("hr"));
// with css
const input = document.createElement("input");
input.classList.add("spaced")
document.body.appendChild(input);
document.body.appendChild(input.cloneNode(1));.spaced { margin-right:5px; }
发布于 2020-06-14 13:43:03
您可以按以下方式添加页边距:
let inp = document.createElement("input");
inp.style.margin='4px';
document.body.appendChild(inp);
inp = document.createElement("input");
inp.style.margin='4px';
document.body.appendChild(inp);
发布于 2020-06-14 13:53:22
输入之间的空格不是空白处,它很可能是空白a空格,所以当您添加一个带有js的元素时,它没有空格,如果您用带有font-size: 0样式的div包装这两个输入,那么即使是在html中添加的输入,也会看到这个空格不皂化。
https://stackoverflow.com/questions/62373303
复制相似问题