我試圖編寫一個腳本來改變頁面的寬度,考慮到用戶的客戶端寬度。 這件事情是這樣的:通過JavaScript更改body標籤樣式
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();
};
有了這個劇本我從螢火蟲得到一個錯誤:
document.getElementsByTagName("body").style is undefined
現在的問題是,我怎麼能進入人體的風格?因爲在CSS工作表中它的選擇器和寬度屬性被定義。
試**的getComputedStyle'(元素)[式]'**和示例**'window.getComputedStyle(document.body的).width' ** .. 。學習http://javascript.info/tutorial/styles-and-classes-getcomputedstyle – KingRider 2016-03-24 12:03:09