如何在按钮之间换行

时间:2018-03-30 19:57:17

标签: javascript html

如何在javascript按钮之间添加换行符?我想要破解标签  在按钮元素之外。

HTML:

<button id="buttonOne"> click one me </button>
<button id="buttonTwo"> click on me too </button>

JS:

var buttonOne = document.getElementById("buttonOne");
var lineBreak = document.createElement("br");
buttonOne.appendChild(lineBreak);

给出:

<button id="buttonOne"> click one me <br> </button>
<button id="buttonTwo"> click on me too </button>

我想要的是:

<button id="buttonOne"> click one me </button> 
<br>
<button id="buttonTwo"> click on me too </button>

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var buttonOne = document.getElementById("buttonOne");
var lineBreak = document.createElement("br");
buttonOne.parentNode.insertBefore(lineBreak, buttonOne.nextSibling);
&#13;
<button id="buttonOne"> click one me </button>
<button id="buttonTwo"> click on me too </button>
&#13;
&#13;
&#13;

尝试:

var buttonOne = document.getElementById("buttonOne");
var lineBreak = document.createElement("br");
buttonOne.parentNode.insertBefore(lineBreak, buttonOne.nextSibling);

答案 1 :(得分:0)

after()应该这样做:

&#13;
&#13;
var buttonOne = document.getElementById("buttonOne");
var lineBreak = document.createElement("br");
buttonOne.after(lineBreak);
&#13;
<button id="buttonOne"> click one me </button>
<button id="buttonTwo"> click on me too </button>
&#13;
&#13;
&#13;