Insert sibling node in JS

前端 未结 2 2011
栀梦
栀梦 2020-12-30 00:37

So I have a div with some pre tags in it, like so:

1


        
2条回答
  •  青春惊慌失措
    2020-12-30 01:10

    You could also insert a new sibling using insertAdjacentElement or insertAdjacentHTML; both of which take the options beforebegin, beforeend, afterbegin and afterend.

    Example:

    var container = document.getElementById('editor'),
    firstChild = container.childNodes[1];
    
    var newPre = document.createElement('pre');
    newPre.setAttribute("contentEditable", "true");
    newPre.innerHTML = "boom";  
    firstChild.insertAdjacentElement("afterend", newPre);
    

提交回复
热议问题