根据不同的UA设置不同的Class值
2021-01-06 20:55:52
jQuery相关
今天仿大前端的时候,发现这个模板手机端浏览跟PC端浏览时,body的Class值竟然不一样,然后就度娘搜索了相关的文章,自己总结了一下,测试暂时没问题,代码如下:
<script type="text/ja
<script type="text/ja
今天仿大前端的时候,发现这个模板手机端浏览跟PC端浏览时,body的Class值竟然不一样,然后就度娘搜索了相关的文章,自己总结了一下,测试暂时没问题,代码如下:
<script type="text/javascript">
function browserRedirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
//电脑端
$('body').addClass('home blog');
}else{
//手机端
$('body').addClass('home blog fixnav-m-on is-phone');
}
}
browserRedirect();//调用判断当前访问页面是手机端还是移动端
</script>
切记,代码一定要放在JQ的引用后面。
<script type="text/javascript">
function browserRedirect() {
var sUserAgent = navigator.userAgent.toLowerCase();
var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
var bIsMidp = sUserAgent.match(/midp/i) == "midp";
var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
var bIsAndroid = sUserAgent.match(/android/i) == "android";
var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
//电脑端
$('body').addClass('home blog');
}else{
//手机端
$('body').addClass('home blog fixnav-m-on is-phone');
}
}
browserRedirect();//调用判断当前访问页面是手机端还是移动端
</script>
切记,代码一定要放在JQ的引用后面。
很赞哦! ()
相关文章
830*140广告