javascript - 未捕获的类型错误 : Cannot read property 'focus' of undefined

标签 javascript jquery jquery-ui

此问题在我的页面加载时发生。

使用以下脚本 -jquery.simplemodal-1.4.3.js -jquery v1.7.1

下面是一个小代码快照,是发生此错误的 simplemodal 内的代码。

 focus: function (pos) {  
 var s = this, p = pos && $.inArray(pos, ['first', 'last']) !== -1 ? pos : 'first';
     // focus on dialog or the first visible/enabled input element
       var input = $(':input:enabled:visible:' + p, s.d.wrap);
       setTimeout(function () {
    input.length > 0 ? input.focus() : s.d.wrap.focus();
    }, 10);
  },

任何我能做的解决这个问题的想法都很好

最佳答案

这是一个老问题,原发布者可能已经解决了他的具体问题。
但是与错误消息相关的一般问题:

  • 未捕获类型错误:无法读取未定义的属性“...”
  • 未捕获的类型错误:无法读取 null 的属性“...”
  • 未捕获的类型错误:无法设置未定义的属性“...”
  • Uncaught TypeError: Cannot set property '...' of null

对许多人来说仍然很重要,并且有一个简单而普遍的答案:

The object of that we try to read property, set property or call a function
- is not declared or
- is declared, but not defined or
- is null

Google Chrome 中的简单测试代码

<script type="text/javascript">
// object obj is not declared, there is no previos 'var obj;'
obj.prop;              // Uncaught TypeError: Cannot read property 'prop' of undefined
obj.prop = "value1";   // Uncaught TypeError: Cannot set property 'prop' of undefined
obj.func();            // Uncaught TypeError: Cannot read property 'func' of undefined
// ------------------------------------------------------------------------
// object 'obj' is declared with:
var obj;
// but it is not defined, there is no value assigned to it (obj = 5 or something else)
obj.prop;              // Uncaught TypeError: Cannot read property 'prop' of undefined
obj.prop = "value1";   // Uncaught TypeError: Cannot set property 'prop' of undefined
obj.func();            // Uncaught TypeError: Cannot read property 'func' of undefined
// ------------------------------------------------------------------------
// object 'obj' is declared and defined. Value is null
var obj = null;
obj.prop;              // Uncaught TypeError: Cannot read property 'prop' of null
obj.prop = "value1";   // Uncaught TypeError: Cannot set property 'prop' of null
obj.func();            // Uncaught TypeError: Cannot read property 'func' of null
// ------------------------------------------------------------------------
// object 'obj' is declared and defined
var obj = {prop: "propertyValue", func: function() {return "returnValue"}}
// there are no errors
</script>

Firefox 中的相同代码:

// object obj is not declared, there is no previos 'var obj;'
obj.prop;              // Uncaught TypeError: obj is undefined
obj.prop = "value1";   // Uncaught TypeError: obj is undefined
obj.func();            // Uncaught TypeError: obj is undefined
-----------------------------------
// object 'obj' is declared with:
var obj;
// but it is not defined, there is no value assigned to it (obj = 5 or something else)
obj.prop;              // Uncaught TypeError: obj is undefined
obj.prop = "value1";   // Uncaught TypeError: obj is undefined
obj.func();            // Uncaught TypeError: obj is undefined
-----------------------------------
// object 'obj' is declared and defined. Value is null
var obj = null;
obj.prop;              // Uncaught TypeError: obj is null
obj.prop = "value1";   // Uncaught TypeError: obj is null
obj.func();            // Uncaught TypeError: obj is null
-----------------------------------
// object 'obj' is declared and defined
var obj = {prop: "propertyValue", func: function() {return "returnValue"}}
// there are no errors

因此浏览器之间的错误消息存在一些差异,但消息很清楚:

The object in not defined or null.

在一般情况下,很容易理解错误消息的确切含义。

但为什么在具体案例中会出现这种情况?

原因可能有很多,但重要的可能是:

  • 调用函数或访问属性

    • 在初始化/定义之前
      如果动态创建的对象(弹出窗口、模式)尚未准备好,则可能会发生这些情况。
    • 销毁后
  • 通过使用新版本的 API 来更改结构,其中属性或函数是

    • 重命名或
    • 转移到其他对象
  • 通过在数组上使用循环:索引不存在,因为

    • 错误的递增/递减或
    • 状况不佳

如何调试?

  • 查找在哪个对象上以及在何处抛出错误
  • 阅读文档以确保,
    • 尝试在正确的地方使用对象,
    • 在使用对象之前调用了所有需要的函数
  • 如果定义了对象,则查看作用域/函数,
  • 如果不是,请查看其调用者的堆栈跟踪...

关于javascript - 未捕获的类型错误 : Cannot read property 'focus' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251036/

相关文章:

JavaScript - 如何使用 for 循环和 Canvas 绘制几个形状之间有空格的形状

javascript - 如何在数组上使用 .charCodeAt?

javascript - 比较 jQuery 中的 2 个元素(按内容)

css - jQuery 日历的 CSS 字体大小问题

javascript - Angular 验证,验证失败时将类添加到输入字段

javascript - Firebug 的 jQuery 不会在我的网站上加载

javascript - 页面刷新后保留下拉列表的选定值

javascript - 隐藏 jsTree 中的复选框

jquery - if 语句更改页面上的 css 位置

javascript - JS 事件委托(delegate)对某些组件不起作用