私は、ユーザーのクライアントの幅を考慮して、ページの幅を変更するスクリプトを作成しようとしています。 それはこのようなものです:このスクリプトではJavaScriptを使用してボディタグスタイルを変更する
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
今、私の質問は、私は身体のスタイルにアクセスする方法、ありますか? CSSシートではselectorとwidthプロパティが定義されています。
試し** 'getComputedStyle(要素)[スタイル]' **と例** 'window.getComputedStyleが(document.body).width' ** .. 。learn http://javascript.info/tutorial/styles-and-classes-getcomputedstyle – KingRider