【问题标题】:$(function() vs jQuery(document).ready [closed]$(function() vs jQuery(document).ready [关闭]
【发布时间】:2018-11-07 11:48:33
【问题描述】:

我不知道为什么 jsGrid$(function() (sample) 中有效,但在 jQuery(document).ready (sample 2) 中无效

我在示例 2 中得到的错误是:Uncaught TypeError: Cannot set property '_grid' of undefined

我需要让它像在sample 2 上一样工作 jQuery(document).ready

【问题讨论】:

  • 你读过What is the difference between these jQuery ready functions?和相关的吗?请在此处添加您的代码而不是 JSfiddle。如果上述错误没有解决,我建议使用“jsGrid not working with jQuery(document).ready”之类的标题
  • 这是一个吊装问题。不能将变量声明放在您尝试访问它们friendscountries 的下面的那个函数中。 var 将被提升,但在您尝试访问它直到分配完成时将未定义。到那时你已经将undefined 传递给插件
  • 简单的修复可能是 getFriends()getCountries() 的辅助函数,因此数据可以驻留在下方并将业务逻辑保持在顶部
  • @charlietfl 非常感谢!现在工作jsfiddle.net/8hw0wcjj/2

标签: javascript jquery user-interface frontend jsgrid


【解决方案1】:

这是因为 countries 未定义。在工作情况下,您在全局范围内定义此变量。但是,在第二个示例中,您在使用此变量创建网格后定义它。

【讨论】:

    猜你喜欢
    • 2011-04-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多