如果用户在移动设备上,则更改网页内容

时间:2011-04-11 16:23:38

标签: jquery html device-detection

我在网站上有一个链接,可以使用jQuery在弹出框中打开iFrame。 jQuery脚本仅将此函数应用于具有特定属性“id = calcPop”的链接,如下所示。

<a href="calculator.html" id="calcPop">Click here</a>

它适用于所有计算机,但在移动设备上非常错误。有没有办法检测用户是否在移动设备上,然后将其更改为没有'id'属性?

1 个答案:

答案 0 :(得分:3)

如果您不能使用像PHP这样的服务器端语言,那么只需使用JS删除ID - 如果你有jQuery,这样的东西就可以解决问题:

$("#calcPop").attr("id", "");

由于有很多移动设备,要检测您是否在移动设备上是相当容易的。

您可以使用以下内容:

var isMobile = navigator.userAgent.match(/Mobile/i) != null;

要在UA中找到移动设备(与iPod / iPad / iPhone匹配),不确定其他设备,您必须检查。

将它放在你的文档中。已经关闭:

var isMobile = navigator.userAgent.match(/Mobile/i) != null;
if (isMobile) {
    $("#calcPop").attr("id", "");
}

在PHP中,您可以执行以下操作:

<?php
$isMobile = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'Mobile');

if ($isMobile) {
    $id = "calcPop";
} else {
    $id = "";
}
?>

<a href="calculator.html" id="<?= $id ?>">Click here</a>