我需要一个代码段来帮助我的元标记仅在移动设备上显示,并且默认桌面将被隐藏。
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 append
<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/