尝试动态设置MetaTag [英] Trying to set MetaTag dynamically
问题描述
我试图动态设置一个元标记到我的文档的头部。这是我需要通过代码添加的移动设备特定元标记。我在这里找到了这个解决方案:
但它似乎不起作用,我做错了什么?
function setOrCreateMetaTag(metaName,name,value){
var t ='meta ['+ metaName +'='+ name + ']';
var mt = $(t);
if(mt.length === 0){
t ='< meta'+ metaName +'=''+ name +'/>';
mt = $(t).appendTo('head');
}
mt.attr('content',value);
$ b $ setOrCreateMetaTag(name,viewport,'width = device-width,initial-scale = 1.0,user-scalable = no,minimum-scale = 1.0,maximum-scale = 1.0' );
首先确保你包含jQuery,因为$()是一个jQuery方法。这意味着包括如下内容:
< script src =http://ajax.googleapis.com/ajax/libs/ jquery / 1.5.2 / jquery.min.jstype =text / javascript>< / script>
第二,我不认为您真的想要传递变量名称和视口。相反,你应该传递这样的字符串:
setOrCreateMetaTag('name','viewport','width = device-width, initial-scale = 1.0,user-scalable = no,minimum-scale = 1.0,maximum-scale = 1.0');
i'm trying to dynamically set a metatag to the head of my document. This is a mobile device specific metatag that I need to add through code. I found this solution here:
Having trouble using jQuery to set meta tag values
but it doesnt seem to work, what am I doing wrong?
function setOrCreateMetaTag(metaName, name, value) {
var t = 'meta['+metaName+'='+name+']';
var mt = $(t);
if (mt.length === 0) {
t = '<meta '+metaName+'="'+name+'" />';
mt = $(t).appendTo('head');
}
mt.attr('content', value);
}
setOrCreateMetaTag(name, viewport, 'width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0');
Two things. First make sure you are including jQuery, since the $() is a jQuery method. This means including something like:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
The second, I don't think you really meant to pass variables name and viewport. Rather, you should pass the strings like:
setOrCreateMetaTag('name', 'viewport', 'width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0');
这篇关于尝试动态设置MetaTag的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!