需要引入 <script src="http://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/mobile-detect/1.4.1/mobile-detect.min.js"></script>
<script> class GetPhone{
// 獲取手機操作系統 static osType(){ const device_type = navigator.userAgent;
// 對數據進行處理 const md = new MobileDetect(device_type); const os = md.os(); return os }; // 獲取安卓品牌 static androidPhone(){ const device_type = navigator.userAgent;
// 獲取手機品牌 const result_list = device_type.split(";"); let index = 0 for (var i = 0; i < result_list.length; i++){ if (result_list[i].indexOf("Build/") > -1){ index = i break } } const model = result_list[index].substring(0, result_list[index].indexOf("Build/")); return model }
// 獲取蘋果品牌
static iosPhone(){
const device_type = navigator.userAgent;
const md = new MobileDetect(device_type);
const os = md.os() + md.version("iPhone");
const model = md.mobile();
return model
}
}
</script>
在這里有些情況下不太准,例如:華為在上邊的哪個位置是 系列名,所以實際情況要判斷下。
參考: https://www.cnblogs.com/justphp/p/8439258.html