3 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
這是一個(gè)片段,可以助您一臂之力:
theParent = document.getElementById("theParent");
theKid = document.createElement("div");
theKid.innerHTML = 'Are we there yet?';
// append theKid to the end of theParent
theParent.appendChild(theKid);
// prepend theKid to the beginning of theParent
theParent.insertBefore(theKid, theParent.firstChild);
theParent.firstChild將為我們提供第一個(gè)元素的引用,theParent并放在其theKid前面。

TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
您在這里沒有給我們太多的工作,但我想您只是在問如何在元素的開頭或結(jié)尾添加內(nèi)容?如果是這樣,這是您可以輕松完成的操作:
//get the target div you want to append/prepend to
var someDiv = document.getElementById("targetDiv");
//append text
someDiv.innerHTML += "Add this text to the end";
//prepend text
someDiv.innerHTML = "Add this text to the beginning" + someDiv.innerHTML;
挺容易。
添加回答
舉報(bào)