【问题标题】:JQuery code working only in Chrome [closed]仅在 Chrome 中工作的 JQuery 代码 [关闭]
【发布时间】:2012-12-22 21:12:26
【问题描述】:

这段代码有什么问题?

$(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);

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

【问题讨论】:

  • 您需要告诉我们出了什么问题。你得到什么错误,你想让它做什么没有做什么?
  • 你想用这个做什么?!
  • 欢迎来到浏览器怪癖的奇妙世界!现在,您能向我们解释一下您想用这个函数完成什么吗?
  • what's wrong with this code 在没有提供其他症状时不是问题
  • @charlietfl,但是...what's wrong with this code? 是个问题!你忽略了他的问号!

标签: jquery google-chrome


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    你应该使用

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-12
      相关资源
      最近更新 更多