如何在桌面上显示时附加普通 jQuery 并在移动设备上显示时附加 jQuery Mobile?
我正在构建一个网站,我想在移动设备上使用 jQuery Mobile 执行 javascript,但由于并非所有桌面浏览器都支持 jQuery Mobile,但我希望附加普通的 jQuery。我该怎么做?
I am building a website that I want to use jQuery Mobile to do the javascript when on mobile devices, but since jQuery Mobile is not supported by all desktop browsers yet I want normal jQuery to be attached. How would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是在客户端执行此操作的一种方法:
Here is one way of doing it on the client:
也许构建一个可能的浏览器类型的枚举,然后针对
window.navigator.userAgent
进行测试?您可以使用 Modernizr 之类的库来测试功能,但这不会为您提供实际的浏览器类型。目前还没有 userAgent 属性来识别浏览器类别(桌面、平板电脑、移动设备等),因此您只能进行愚蠢的 UA 比较。像 51 Degrees 这样的项目旨在简化此评估,但它们会产生隐性成本,即 51 Degrees 需要10 秒评估客户端浏览器并相应地重定向。当然,就您而言,您需要条件样式,而不是重定向。这表明
window.navigator.userAgent
检查可能适合if(true) document.write('')
类型方法。Perhaps build up an enum of probable browser types and then test against
window.navigator.userAgent
? You can use a library like Modernizr to test for features, but this won't get you the actual browser type. There is no (yet) userAgent prop to identify browser class (desktop, tablet, mobile, etc.), so you're stuck doing a dumb UA comparison.Projects like 51 Degrees aim to simplify this evaluation, but they incur hidden costs--namely, 51 Degrees takes upwards of 10 seconds to evaluate the client browser and redirect accordingly. Of course, in your case, you want conditional styling--not redirect. This suggests that a
window.navigator.userAgent
check might lend itself to anif(true) document.write('<javascript resource />')
type approach.您必须在服务器上测试浏览器请求标头(用户代理),然后相应地绑定框架:
例如参见
http://pgl.yoyo.org/http/browser-headers.php
用户代理:Mozilla/5.0(Windows;U;Windows NT 6.1;de;rv:1.9.2.18)Gecko/20110614 Firefox/3.6.18
You'll have to test the browser request header (User-Agent) on the server and then bind the framework accordingly:
e.g. see
http://pgl.yoyo.org/http/browser-headers.php
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.18) Gecko/20110614 Firefox/3.6.18