0

如果我有一些 HTML 文档的正文如下:

<body>
    <p>Here's some text</p>
</body>

然后以下将文本附加到<body>

var jselem = document.createElement("div");
jselem.innerHTML = '<p>Some other text here</p>';
document.body.appendChild(jselem);

...导致:

Here's some text
Some other text here

有没有办法在动态创建以下结果后生成div以下内容?

Some other text here
Here's some text
4

1 回答 1

4

我想你在问 DOM method insertBefore

document.body.insertBefore(jselem, document.body.firstChild);

演示:http: //jsfiddle.net/MCnxM/

于 2013-06-08T17:58:23.577 回答