Javascript jQuery 检查平板电脑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10300875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:44:44  来源:igfitidea点击:

jQuery Check for tablet?

javascriptjquery

提问by panthro

I use a media query to load in a stylesheet (if device is a tablet).

我使用媒体查询加载样式表(如果设备是平板电脑)。

I was just wondering how I could check in my javascript if the user is using a tablet, is there a check or should I just check what stylesheet was loaded in?

我只是想知道如果用户使用的是平板电脑,我如何检查我的 javascript,是否有检查或者我应该检查加载了什么样式表?

采纳答案by Alucardz

Try to see if perhaps this sheds a light on your question:

试着看看这是否能说明你的问题:

What is the best way to detect a mobile device in jQuery?

在 jQuery 中检测移动设备的最佳方法是什么?

As the answer to that thread says, I'd suggest to rely on 'feature detection' rather than tablet vs non-tablet (as it may not be as straightforward as one may think) hence --> http://modernizr.com/

正如该线程的答案所说,我建议依靠“功能检测”而不是平板电脑与非平板电脑(因为它可能不像人们想象的那么简单)因此 --> http://modernizr.com /

回答by Ballbin

You can use navigator.userAgent and do a substring search to find out which tablet viewing your site. Here is a current list of tablet userAgent http://mojosunite.com/tablet-user-agent-strings

您可以使用 navigator.userAgent 并进行子字符串搜索以找出查看您网站的平板电脑。这是平板电脑用户代理的当前列表 http://mojosunite.com/tablet-user-agent-strings

回答by Surreal Dreams

Try passing the useragent from the server into the page so that JavaScript knows exactly what browser is being reported.

尝试将用户代理从服务器传递到页面,以便 JavaScript 确切知道正在报告的浏览器。