※ 引述《Nitschke (下载至死)》之铭言:
直接用你给的范例做修改:
http://codepen.io/arthow4n/pen/JRJkGw
var targets = document.querySelectorAll('p br:first-of-type');
[].forEach.call(targets, function (e) {
e.insertAdjacentText('beforebegin', ';')
});
首先选择你所有的 p 其下所有的第一个 br
(利用:first-of-type来避开是:first-child的<span>),
再来对所有选中的dom节点(那些<br>)进行loop
([].forEach.call只是偷懒的方法,for循环也可去loop那个targets),
对每一个选取到的元素进行原生的 insertAdjacentText 插入文字节点。
插的位置也可以改动,参考:
https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentText