你好,现在我的 div 后面有 3 个东西。
一个按钮和两个图像。我想做的是让他们有一个新行,这样按钮就在一行中,图像在他们自己的行中,第二个图像也在他们自己的行中。
我试过document.write("<BR>")
但它完全破坏了我的代码。这是当前的代码
var button = document.createElement("button");
button.innerHTML = "Do Something";
document.body.appendChild(button)
var img7 = document.createElement('img')
img7.src = 'g.png'
document.body.appendChild(img7)
var img8 = document.createElement('img')
img8.src = 'h.png'
document.body.appendChild(img8)
它是纯JavaScript的,所以我不能使用<br>
换行
最佳答案
您可以创建并附加 <br>
在 JavaScript 中,就像您对每个 <img>
所做的那样:
var button = document.createElement("button");
button.innerHTML = "Do Something";
document.body.appendChild(button)
var br = document.createElement('br')
document.body.appendChild(br)
var img7 = document.createElement('img')
img7.src = 'g.png'
document.body.appendChild(img7)
document.body.appendChild(br.cloneNode())
var img8 = document.createElement('img')
img8.src = 'h.png'
document.body.appendChild(img8)
关于javascript - 如何用javascript模仿html BR标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43175259/