在标签内容之前插入动态创建的 div

0 html javascript dom

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

<body>
    <p>Here's some text</p>
</body>
Run Code Online (Sandbox Code Playgroud)

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

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

...导致:

Here's some text
Some other text here
Run Code Online (Sandbox Code Playgroud)

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

Some other text here
Here's some text
Run Code Online (Sandbox Code Playgroud)

pvn*_*ula 5

我认为你问的是 DOM 方法insertBefore

document.body.insertBefore(jselem, document.body.firstChild);
Run Code Online (Sandbox Code Playgroud)

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