Add text before or after an HTML element

前端 未结 6 2015
误落风尘
误落风尘 2020-12-09 02:34

If i have an HTML element like

with some text inside or another elements can I add before or after this div some text data without an html element,
6条回答
  •  情书的邮戳
    2020-12-09 02:59

    Yes, you can create a text node with document.createTextNode('the text')

    Then you can insert it like an element, with appendChild or insertBefore.

    Example that insert a text before #childDiv:

    var text = document.createTextNode('the text');
    var child = document.getElementById('childDiv');
    child.parentNode.insertBefore(text, child);
    

提交回复
热议问题