gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
js判断是否是移动端
- User
js判断是否是移动端
- Assistant
可以使用JavaScript来判断是否是移动端设备。以下是一种常见的判断方法:
function isMobile() { return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); }这个函数会检查
navigator.userAgent中的字符串是否包含移动设备的关键词(如Android、iPhone等)。如果返回true,则表示是移动端设备;如果返回false,则表示是桌面端设备。可以在需要判断的地方调用这个函数,例如:
if (isMobile()) { console.log("这是移动端设备"); } else { console.log("这是桌面端设备"); }