我正在使用mdetect.js
从 mobileesp.com 获取。该文件看起来非常适合我的需要,我已将其保存在本地。然后在我的 HTML 页面上,我想调用其中一个函数,如果它返回 true(即用户使用移动设备),则会在页面顶部显示一个表格,说明我们的网站当前不支持移动设备。然而,在到达那里之前我就已经失败了:
<html>
<head>
<script type="text/javascript" src="mdetect.js"></script>
</head>
<body>
<h1>header 1</h1>
<script type="text/javascript">
document.write(DetectIphoneOrIpod());
</script>
<p>this is normal body text</p>
</body>
</html>
我做错了什么?我什至还没有接触到复杂的东西(至少对我来说)。为什么它不根据该函数的返回值来写 true 或 false?如有任何帮助,我们将不胜感激。
谢谢
最佳答案
我认为你想做的是这样的:
<html>
<head>
<script type="text/javascript" src="mdetect.js"></script>
</head>
<body>
<h1>header 1</h1>
<script type="text/javascript">
if (DetectIphoneOrIpod()) {
document.write("<table>...</table>");
}
</script>
<p>this is normal body text</p>
</body>
</html>
但是,您可能希望使用 DetectSmartphone()
或 DetectMobileQuick()
函数来检测更多移动设备,而不仅仅是 iPhone 和 iPod Touch。
关于javascript - 为什么我的 JavaScript 无法检测到移动设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10369234/