如何使用jQuery来检测用户的设备
任务是使用JQuery来确定用户的设备,是否是iPad。以下是所需的方法。
- 导航仪userAgent属性。
该属性返回由浏览器发送给服务器的用户代理头的值。
返回的值,有浏览器的名称、版本和平台等信息。
语法:
navigator.userAgent
返回 value:
它返回一个字符串,代表当前浏览器的用户代理字符串。
例子:这个例子使用navigator.userAgent属性来识别用户的设备。
<!DOCTYPE HTML>
<html>
<head>
<title>
JQuery
| Detect iPad users.
</title>
</head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js">
</script>
<body style="text-align:center;"
id="body">
<h1 style="color:green;">
GeeksForGeeks
</h1>
<p id="GFG_UP"
style="font-size: 17px;
font-weight: bold;">
</p>
<button>
click here
</button>
<p id="GFG_DOWN"
style="color: green;
font-size: 24px;
font-weight: bold;">
</p>
<script>
('#GFG_UP').text(
"Click on the button to see "+
"whether user's device is iPad or not");
('button').on('click', function() {
var is_iPad =
navigator.userAgent.match(/iPad/i) != null;
$('#GFG_DOWN').text(is_iPad);
});
</script>
</body>
</html>
输出:
- 在到达底部之前。
- 到达底部后。