所以我尝试使用 jquery 获取设备的宽度 我制作了具有 2 个接口(interface)的笔记本电脑和手机的响应式网站,但问题是它在移动设备上不起作用,即使 div 测试的尺寸在手机上超出尺寸请帮助我,我用于重定向的代码在下面带有注释
<html>
<head>
<script src="./js/jquery-3.2.1.js"></script>
</head>
<body>
<div id='test' style=" width: 2.54cm; height:2.54cm;"></div>
//this div dimension works on laptop it measure exactly 2.54cm or 1nch
//using ruler but in mobiles it shrink and out of measure
<script>
//as you see I get the width of the div to get the pixel per inch
var sqw=$('#test').width();
var sqh=$('#test').height();
//and I get the total width of the document
var dheight=$(document).height();
var dwidth=$(document).width();
var w=dwidth/sqw;
//and I divide the total width of the document to pixel per inch
//the problem is the mobile show 10.98 inch where the mobile I use is
//miniFlare S5 cherrymobile with width of just 2.5 inches
if(w<=7) {
window.location.replace("./m_index.php");
} else {
window.location.replace("./d_index.php");
}
</script>
</body>
</html>
最佳答案
您正在使用 jQuery。 From the docs .width() they say :
This method is also able to find the width of the window and document.
// Returns width of browser viewport
$( window ).width(); // <- this is the one you are looking for!
// Returns width of HTML document
$( document ).width();
关于javascript - 如何使用javascript获取手机的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49125355/