简体   繁体   中英

How do I use Node.insertBefore to add content to a specific node

I am trying to add a string variable as a child of a node. The code that I'm using looks like this

$(this).parentNode.parentNode.insertBefore('content',$(this).parentNode)

I believe that this is correct syntax, but I keep receiving NOT_FOUND_ERR: DOM Exception 8. Does anyone have any pointers?

parentElement.insertBefore(el, beforeWhat);

if you want to insert a new element BEFORE a node

if you want to append a new textNode to the element you rather need

var textNode = document.createTextNode("content");
el.appendChild(textNode);

but what really bothers me is that you seem to using jQuery or some framework, and use DOM methods on them. Because that won't work.

You need to use their own methods then, like:

$(this).append("content");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM