简体   繁体   中英

How should I append a newly created element to the next to last position of another element using Javascript?

I want to append a newly created div into the next to last position of an element but I am a beginner and I'm not quite sure how to select that position.

var div = document.createElement("div");
div.className = "test";
div.innerHTML = "some stuff";
document.getElementById('content').appendChild(div);

Something like

var div = document.createElement("div");
div.className = "test";
div.innerHTML = "some stuff";
var parent    = document.getElementById('content');
var children  = parent.children;

parent.insertBefore(div, children[children.length-1]);

FIDDLE

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