『壹』 js裡面獲取屏幕寬高的screen對象,為什麼獲取不了屏幕的寬高值
<!DOCTYPEhtml>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>
RunJS演示代碼
</title>
<styletype="text/css">
body,div{
margin:0px;
padding:0px;
}
#show{
width:400px;
height:400px;
margin:100pxauto;
background:#969696;
text-align:center;
}
</style>
<scripttype="text/javascript">
functionmess(){
varstr="屏幕尺寸基本信息:<br/>";//這里你是中文的分號,故而報錯
str+="屏幕寬度:"+screen.width+"像素<br/>";
str+="屏幕高度:"+screen.height+"像素<br/>";
str+="屏幕可用寬度"+screen.availWidth+"像素<br/>";
str+="屏幕可用高度"+screen.availHeight+"像素<br/>";
document.getElementById("show").innerHTML=str;
}
</script>
</head>
<bodyonload="mess()">
<divid="show">
</div>
</body>
</html>
『貳』 JavaScript中如何獲取手機瀏覽器的大小
JavaScript不管是判斷PC瀏覽器還是手機瀏覽器,都是通過User Agent 來判斷
var ua = navigator.userAgent;
取得大小可內以試下
var screenAW = window.screen.availWidth;
var screenAH = window.screen.availHeight;
或者容
var screenW = window.screen.width;
var screenH = window.screen.height;