我在我的网站上使用这个 PHP 库:http ://code.google.com/p/php-mobile-detect/
我推荐这个而不是 JavaScript 库,因为这意味着在移动设备上要做的处理更少,而且你的网页代码更干净,没有针对多个设备的条件语句。
需要注意的是(如果您想这样称呼它)是您有两个版本的网站需要维护。我自己并不认为这是一个问题,尽管当权衡使用条件语句来决定要显示哪些内容的网页时。我的根目录中有一个桌面文件夹和一个移动文件夹。
我只是使用该isMobile
方法并忽略其余大部分内容,但如果您愿意,它可用于过滤和定位特定设备类型。
例如用法:
// Include the mobile detection file.
require('Mobile_Detect.php');
// Create new mobile detect object.
$detect = new Mobile_Detect();
// Create new browser object
$browser = new Browser();
// Check if the client is using a mobile device.
$mobile = $detect->isMobile();
...
if($mobile) {
//Load mobile site version
require('Mobile\\index.html');
} else {
//Load desktop site version
require('Desktop\\index.html');
}
在您的情况下,如果您只想更改数字,JavaScript 可能是更好的解决方案。这个答案可能更适合那些希望开发一个适合移动设备的整个网站的人。