javascript - 如何使用javascript获取手机的宽度

标签 javascript jquery html css

所以我尝试使用 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/

相关文章:

javascript - 在 JavaScript 中使用 XMLHttpRequest Get 方法将变量数据发送到服务器?

javascript - 在加载子行时显示进度

javascript - 如何使用 angular.js 在 DOM 周围移动 View ?

javascript - 为什么 jQuery 在点击事件时中断?

c# - 从代码后面带参数调用 javascript 函数

php - 从 HTML 网页中的 Google Maps iframe 获取坐标

javascript - jump-player-tab-container.js :129 Uncaught (in promise) ReferenceError: isActive is not defined(…)render @ jump-player-tab-container. js:129

javascript - 隐藏/显示表格排序器的过滤器

jquery - MVC 3 - 通过jquery提交表单并部分页面刷新以显示发布后的结果

html - 更改输入文本边框颜色而不更改其高度