使用 JQuery 在 Head 标签中添加元数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11018638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:05:56  来源:igfitidea点击:

Using JQuery to add metadata in the Head Tag

jquery

提问by Yippyj

I'm trying to insert a new meta tag within the Head, I'm using a content managment system that won't allow editing within the Head so I'm attempting to do this using jQuery. Unfortunately I can't it to work.

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

This is the code I've added to the following webpage: http://www.newcastlegateshead.com

这是我添加到以下网页的代码: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>

回答by thecodeparadox

Try your code within

在里面试试你的代码

<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>

Short form of $(document).ready(function() {...})is:

的简短形式$(document).ready(function() {...})是:

jQuery(function ($) {
    // Your code
})

This will ensure that you code will execute during onload.

这将确保您的代码将在onload.

回答by A.J. Hernandez

This worked for me ... thank you @thecodeparadox. For me to get it to work I needed single quotes notdouble quotes before and after

这对我有用......谢谢@thecodeparadox。为了让它工作,我需要单引号而不是前后双引号

<script type="text/javascript"> 
     $(document).ready(function() {
         $("head").append('<meta name="apple-mobile-web-app-capable" content="yes">');
     });
</script>