简体   繁体   English

上面写代码 <header> 使用Javascript标记

[英]Write code above <header> tag using Javascript

I'm putting a javascript on a website, I also need to add an open graph tag above the tag of the page. 我正在网站上放置JavaScript,还需要在页面标签上方添加一个开放图形标签。 Is there a way to add the code there using document.write or any other javascript method rather than asking the user to add it by himself which is not easy for a person without html knowledge. 有没有一种方法可以使用document.write或任何其他javascript方法在其中添加代码,而不是要求用户自己添加代码,这对没有html知识的人来说并不容易。

Thanks. 谢谢。

This is the example code to add a paragraph before an element with ID someID : 这是在ID为someID的元素之前添加段落的示例代码:

   var writeinTo = document.getElementsByID('someID');
   var para = document.createElement('p');
   writeinTo.parentNode.insertBefore(para, writeinTo);

Take a look at insertBefore . 看一下insertBefore If you're using jQuery, you can do it like this: 如果您使用的是jQuery,则可以这样做:

$('<meta name="foo" content="bar">').insertBefore('head');

Updated example because I thought the OP meant the <header> element, not <head> . 更新了示例,因为我认为OP表示<header>元素,而不是<head>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM