本文介绍了在< / body>之前插入元素用香草JavaScript标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<html>
  ...
  <body>
    ...
  // element should be inserted here
  </body>
</html>

我对vanilla Javascript不太熟悉,总是使用jQuery。我到目前为止尝试了这个,但是元素位于< head> < body> 的中间。

I'm not very familiar with vanilla Javascript, always have worked with jQuery. I tried this so far, but that got the element in the middle of <head> and <body>.

var bodyTag = document.getElementsByTagName('body')[0];
bodyTag.parentNode.insertBefore(myElement, bodyTag);


推荐答案

这很简单。使用方法,可以写成:

It's pretty simple. Using appendChild method it can be written as short as:

document.body.appendChild(myElement);

这篇关于在&lt; / body&gt;之前插入元素用香草JavaScript标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:25
查看更多