使用JQuery在Head Tag中添加元数据

Yip*_*pyj 10 jquery

我正在尝试在Head中插入一个新的元标记,我正在使用内容管理系统,该系统不允许在头部内进行编辑,因此我尝试使用jQuery执行此操作.不幸的是我无法工作.

这是我添加到以下网页的代码:http://www.newcastlegateshead.com

<script type="text/javascript"> 
$("head").append("<meta name=viewport content=width=400, initial-scale=0.45, minimum-    scale=0.45/><link rel=apple-touch-icon href=/images/customIcon.png/><meta name=apple-mobile-web-app-capable content=no /><meta name=apple-mobile-web-app-status-bar-style content=black-translucent /><link rel=apple-touch-icon-precomposed href=/images/customIcon.png/> ");
</script>
Run Code Online (Sandbox Code Playgroud)

the*_*dox 15

试试你的代码

<script type="text/javascript"> 
     $(document).ready(function() {
         $("head").append("<meta name=viewport content=width=400, initial-scale=0.45, minimum-    scale=0.45/><link rel=apple-touch-icon href=/images/customIcon.png/><meta name=apple-mobile-web-app-capable content=no /><meta name=apple-mobile-web-app-status-bar-style content=black-translucent /><link rel=apple-touch-icon-precomposed href=/images/customIcon.png/> ");
    });
</script>
Run Code Online (Sandbox Code Playgroud)

简称$(document).ready(function() {...})是:

jQuery(function ($) {
    // Your code
})
Run Code Online (Sandbox Code Playgroud)

这将确保您的代码将在执行期间执行onload.

  • @Yippyj 如果您有萤火虫,则在页面加载后打开它的“控制台”并执行上面的代码,然后检查您的 dom,您会发现新添加的元标记 (2认同)