JQuery代码仅适用于Chrome

时间:2012-12-22 21:12:26

标签: jquery google-chrome

这段代码出了什么问题?

$(function size() {
    var width = document.width;
    $('.section').css('width', 2 * width);
    $('body').css('width', 8 * width).css('font-size', 0.023 * width);
    $('.center').css('width', width);
    $('#title').css('width', 0.78 * width);
    $('#lf').css('width', 0.56 * width);
});

$(window).ready(size);

请帮忙!我不知道出了什么问题:/

2 个答案:

答案 0 :(得分:0)

document.width在MDN中标记为已过时,请改用document.body.clientWidth

$(window).ready(...)也不是有效的jquery,你的意思是$(window).load()吗?

虽然第一个代码块是以与$(document).ready(...)相同的方式注册处理程序,但是不需要第二个调用。

答案 1 :(得分:0)

你应该使用

var width = $(document).width();

而不是

var width = document.width;

在Chrome中var width = document.width;返回宽度,但在其他浏览器中返回undefined。由于您使用的是jQuery,因此请充分利用它。您也可以使用

之类的代码
function size() {
    var width = $(document).width();
    alert(width);
};
$(size); // calls ready event event
相关问题