【问题标题】:Cannot insert the meta tag at the beginning of a head无法在头部的开头插入元标记
【发布时间】:2019-08-02 09:49:01
【问题描述】:

我正在尝试将脚本添加到反应应用程序的头部开头。 我能想到的唯一方法是将以下内容添加到组件确实挂载:

    var head = document.getElementsByTagName("head")[0];
    head.insertBefore("<meta  id='test'/>", head.firstElementChild);
    console.log("test");
    console.log(head);

我在这里找到的:

How do I insert a script tag in to the top/beginning of head tag?

但我看不到任何东西插入头部。 有什么想法吗?

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

insertBefore() 接受 Element,而不是字符串。

您需要使用document.createElement() 创建一个Element

【讨论】:

    猜你喜欢
    • 2016-04-06
    • 1970-01-01
    • 2012-10-10
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多