Tôi đang cố viết kịch bản để thay đổi chiều rộng của trang, xem xét chiều rộng của khách hàng của người dùng. Đó là một cái gì đó như thế này:Thay đổi kiểu thẻ body thông qua 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();
};
Với kịch bản này, tôi nhận được một lỗi từ Firebug:
document.getElementsByTagName("body").style is undefined
Bây giờ câu hỏi của tôi là, làm thế nào tôi có thể truy cập vào phong cách của cơ thể? Bởi vì trong trang css, thuộc tính selector và width của nó được định nghĩa.
thử ** 'getComputedStyle (yếu tố) [kiểu] '** và ** dụ' window.getComputedStyle (document.body) .width' ** .. Tìm hiểu http://javascript.info/tutorial/styles-and-classes-getcomputedstyle – KingRider