【问题标题】:jquery define object outside the domcument.ready()jquery 在 document.ready() 之外定义对象
【发布时间】:2011-06-14 03:32:14
【问题描述】:

我在 jquery document.ready 调用之外创建了一个对象,然后从 document.ready 内部调用该对象方法。它在 Firefox 上运行良好,但在 chrome 中出现错误。显然,如果我将该对象放在 document.ready 中,它可以正常工作,但是我将无法从 document.ready 外部调用该对象。所以我需要解决这个问题。以下是代码

var status = {
       method_one: function() { ...},
       method_two: function() { ...}
}

jquery(function() { // document ready
    status.method_one(); // giving error here in chrome, but does not in firefox.
});

【问题讨论】:

  • 可能只是拼写错误,但应该是jQuery或$,区分大小写。
  • 实际上它工作正常,如果我将其重命名为 feed 或其他任何状态。那真的是有线的。

标签: javascript jquery object methods load


【解决方案1】:

在 Chrome 中对我来说很好用:http://jsfiddle.net/5s739/

您是否自己设置了jquery 值?或许应该是jQuery,大写Q?

var status = {
    method_one: function() { alert(1); },
    method_two: function() { alert(2); }
}

jQuery(function() {
    status.method_one();
});

您可以通过检查 $ == jqueryjQuery == jquery 来快速测试,除非您自己分配。

【讨论】:

  • 实际上它工作正常,如果我将其重命名为 feed 或其他任何状态。那真的是有线的。
【解决方案2】:

怎么样:

var status = null;

jquery(function() { // document ready
    status = {
       method_one: function() { ...},
       method_two: function() { ...}
    }
    status.method_one(); // giving error here in chrome, but does not in firefox.
});

你仍然可以在 document.ready() 之外调用 status

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2012-03-28
    • 1970-01-01
    相关资源
    最近更新 更多