我可以动态更改移动版Safari中的视口元标记吗? [英] Can I change the viewport meta tag in mobile safari on the fly?

查看:136
本文介绍了我可以动态更改移动版Safari中的视口元标记吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为移动版Safari构建的ajax应用程序,需要显示不同类型的内容。一些内容我需要用户可扩展= 1其他内容我需要user-scalable = 0。有没有办法在不刷新页面的情况下即时更新?

I have an ajax app built for mobile Safari that needs to display different types of content. Some content I need user-scalable=1 other content I need user-scalable=0. Is there a way to update this on the fly without refreshing the page?

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;" />


推荐答案

我意识到这有点旧,但是,是的可以办到。一些javascript让你入门:

I realize this is a little old, but, yes it can be done. Some javascript to get you started:

viewport = document.querySelector("meta[name=viewport]");
viewport.setAttribute('content', 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0');

只需更改您需要的部件,Mobile Safari将尊重新设置。

Just change the parts you need and Mobile Safari will respect the new settings.

更新:

如果您在源代码中还没有元视口标记,则可以直接附加如下内容:

If you don't already have the meta viewport tag in the source, you can append it directly with something like this:

var metaTag=document.createElement('meta');
metaTag.name = "viewport"
metaTag.content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
document.getElementsByTagName('head')[0].appendChild(metaTag);

或者如果你正在使用jQuery:

Or if you're using jQuery:

$('head').append('<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">');

这篇关于我可以动态更改移动版Safari中的视口元标记吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆