当我的页面加载时,会发生此问题。
使用以下脚本
-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);
},
我可以解决这个问题的任何想法都很棒
最佳答案
这是一个古老的问题,原始海报可能已经解决了他的具体问题。
但是与错误消息有关的一般问题:
和
对于许多人来说仍然很重要,并且有一个简单而笼统的答案:
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
因此,浏览器之间的错误消息有所不同,但是消息很清楚:
在一般情况下,很容易理解错误消息的确切含义。
但是,为什么在具体情况下会发生这种情况?
原因可能很多,但重要的原因可能是:
如果动态创建的对象(弹出窗口,模式窗口)尚未准备好,则可能会发生这些情况。
如何调试?
关于javascript - 未捕获的TypeError : Cannot read property 'focus' of undefined,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23251036/