gpt4 book ai didi

javascript - jquery 在 document.ready() 之外定义对象

转载 作者:行者123 更新时间:2023-11-28 02:46:40 25 4
gpt4 key购买 nike

我在 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.
});

最佳答案

在 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 来快速测试它,除非您自己分配它。

关于javascript - jquery 在 document.ready() 之外定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4776064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com