如何在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>
答案 0 :(得分:1)
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;
尝试:
var buttonOne = document.getElementById("buttonOne");
var lineBreak = document.createElement("br");
buttonOne.parentNode.insertBefore(lineBreak, buttonOne.nextSibling);
答案 1 :(得分:0)
after()
应该这样做:
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;