gpt4 book ai didi

javascript - 元标签如何只显示在移动设备上?

转载 作者:行者123 更新时间:2023-12-03 02:04:35 24 4
gpt4 key购买 nike

我需要一个代码片段来帮助我的元标记仅在移动设备上显示,并且默认桌面将被隐藏。

function initScreen() {
isMobile = (/iPhone|iPod|Android|BlackBerry/).test(navigator.userAgent);
isTablet = (/iPad/).test(navigator.userAgent);
if(isMobile) {
$('<meta name="viewport" content="initial-scale=0.30, maximum-scale=0.50, minimum-scale=0.25, width=device-width, user-scalable=yes">').appendTo('head');
} else if(isTablet) {
$('<meta name="viewport" content="initial-scale=0.95, maximum-scale=0.95, width=device-width, user-scalable=no">').appendTo('head');
} else {
$('<meta name="viewport" content="initial-scale=0.80, maximum-scale=0.80, width=device-width, user-scalable=no">').appendTo('head');
}
}


<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
$(document).ready(function() {
initScreen();
});
</script>
</head>

有人可以帮助我吗?

最佳答案

也许您需要使用document.write而不是使用jquery追加

<head>
<script>
var isMobile = (/iPhone|iPod|Android|BlackBerry/).test(navigator.userAgent);
isTablet = (/iPad/).test(navigator.userAgent);
if(isMobile) {
document.write('<meta name="viewport" content="initial-scale=0.30, maximum-scale=0.50, minimum-scale=0.25, width=device-width, user-scalable=yes">');
} else if(isTablet) {
document.write('<meta name="viewport" content="initial-scale=0.95, maximum-scale=0.95, width=device-width, user-scalable=no">');
} else {
document.write('<meta name="viewport" content="initial-scale=0.80, maximum-scale=0.80, width=device-width, user-scalable=no">');
}
</script>
</head>

关于javascript - 元标签如何只显示在移动设备上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49853953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com