使用 jquery 确定屏幕宽度

Using jquery to determine screen width

无法在 iphone 上将此脚本正确地 运行 6。不断出现 "not mobile"。我错过了什么?

$(document).ready(function(){
    if ($(window).width < 700){
        alert("mobile");
    }
    else {
        alert("not mobile");
    }
});

编辑:抱歉,我在此处输入的代码有错别字,但这不是我出现问题的原因。我有关于 iphone 分辨率的不准确信息。谢谢大家!

iPhone6 显示器的分辨率为 1334x750。在 chrome 开发工具中模拟 iPhone6 时,宽度报告为 980(我不知道这是否准确)。

您可能对此感兴趣:http://detectmobilebrowsers.com/

此外,正如其他人指出的那样,将 $(window).width 替换为 $(window).width()

JQuery 使用 $(window).width()。这是一个函数,而不是 属性.

好吧,忽略屏幕分辨率,你好像忘记了width后面的括号,那是方法,不是字段。要解决此问题,只需在其后添加 ()

if ($(window).width() < 700)

有关详细信息,请参阅 the documentation for width()

你会想要 .width(),而不仅仅是 .width。另外,记录它并确保它符合您的预期。

iPhone6 屏幕为 1334x750 像素。 如果您仅使用宽度来检测移动用户,请参阅 this

本帖深入探讨 Javascript 和 JQuery

中的选项

Get the size of the screen, current web page and browser window

如果您正在使用 bootstrap 向屏幕添加一个仅在特定断点处显示的元素 (bootstrap 4):

<div id="IsMobile" class="d-block d-lg-none"></div>

那么如果它可见:

if ($("#IsMobile").is(":visible")) {
     //Do Something...
}