我想编写一个脚本来根据用户的客户端宽度改变页面宽度。
function adjustWidth() {
width = 0;
if (window.innerHeight) {
width = window.innerWidth;
} else if (document.documentElement && document.documentElement.clientHeight) {
width = document.documentElement.clientWidth;
} else if (document.body) {
width = document.body.clientWidth;
}
if (width < 1152) {
document.getElementsByTagName("body").style.width="950px";
}
if (width >= 1152) {
document.getElementsByTagName("body").style.width="1075px";
}
}
window.onresize = function() {
adjustWidth();
};
window.onload = function() {
adjustWidth();
};
使用这个脚本,我从Firebug得到一个错误:
document.getElementsByTagName("body").style is undefined
我的问题是如何访问<body>的样式?因为在CSS表中已经定义了它的选择器和宽度属性。
getComputedStyle(elements).[style]
和示例window.getComputedStyle(document.body).width
... 学习请访问 http://javascript.info/tutorial/styles-and-classes-getcomputedstyle - KingRider