How can I implement prepend and append with regular JavaScript without using jQuery?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
Perhaps you're asking about the DOM methods
appendChild
andinsertBefore
.This is not best way to do it but if anyone wants to insert an element before everything, here is a way.
Here's an example of using prepend to add a paragraph to the document.
result:
I added this on my project and it seems to work:
Example:
If referenceElement is null, or undefined, newElement is inserted at the end of the list of child nodes.
Examples can be found here: Node.insertBefore
In order to simplify your life you can extend the
HTMLElement
object. It might not work for older browsers, but definitely makes your life easier:So next time you can do this: