javascript SCRIPT5007:无法获取属性“0”的值:对象为空或未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14412180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:34:26  来源:igfitidea点击:

SCRIPT5007: Unable to get value of the property '0': object is null or undefined

javascriptjqueryinternet-explorer-9fileapi

提问by PleaseStand

I ran this code through Chrome and Firefox and it works well. Whenever I run it through Internet Explorer for testing purposes it will not work. I checked the javascript consule and I'm getting: SCRIPT5007: Unable to get value of the property '0': object is null or undefined. Can someone please tell me why this is coming up and what I can do to fix it?

我通过 Chrome 和 Firefox 运行了这段代码,效果很好。每当我通过 Internet Explorer 运行它以进行测试时,它都不起作用。我检查了 javascript consule,我得到:SCRIPT5007: Unable to get value of the property '0': object is null or undefined。有人可以告诉我为什么会出现这种情况以及我可以做些什么来解决它?

$('#audiofile1').bind('change', function () {
    if (this.files[0].type != 'image/png') {
        $('#audiofile1').each(function () {
            $(this).after($(this).clone(true)).remove();
        });
        alert(this.files[0].name + ' is not a valid file type.');
    } else {
        if (this.files[0].size > '5000') {
            $('#audiofile1').each(function () {
                $(this).after($(this).clone(true)).remove();
            });
            var size = this.files[0].size;
            var maxSize = 100;
            var exceedingSize = size - maxSize;
            alert(this.files[0].name + ' exceeds the maximum file size');
        } else {
            $("#audiofile1").fadeTo(1500, 0.20);
            alert(this.files[0].name + ' was added successfully.');
        }
    }
});

回答by PleaseStand

if (this.files[0].type != 'image/png') {

Inside your event handler, jQuery makes thisthe DOM element for which the handler was registered. Either that element does not have a defined filesproperty, or that property's value is nullor undefined.

在您的事件处理程序中,jQuery 生成this已注册处理程序的 DOM 元素。该元素没有定义的files属性,或者该属性的值为nullundefined

This is because Internet Explorer 9 and below do not support the File API. You should probably just skip binding the handler if the File API is not available:

这是因为 Internet Explorer 9 及以下版本不支持File API。如果 File API 不可用,您可能应该跳过绑定处理程序:

if ($('#audiofile1')[0].files) {
    $('#audiofile1').bind('change', function () {
        // ...
    });
}

回答by Ilmari Karonen

Apparently, IE only supports the HTML5 File API starting with version 10.Thus, in IE 9, there is no .filesproperty to access at all.

显然,IE 仅支持从版本 10 开始的 HTML5 File API。因此,在 IE 9 中,根本没有.files可访问的属性。

Unfortunately, I don't know any way to work around this, except to tell your users to upgrade and/or to provide some alternative (likely server-side) method for users on older browsers to accomplish whatever it is you're doing. In your case, assuming that you're eventually uploading the files to the server for further processing anyway, you could just skip the client-side checks on browsers that don't support them, and simply tell your users what kind of files will be accepted.

不幸的是,我不知道有什么方法可以解决这个问题,除了告诉你的用户升级和/或为旧浏览器上的用户提供一些替代(可能是服务器端)方法来完成你正在做的任何事情。在您的情况下,假设您最终将文件上传到服务器以进行进一步处理,您可以跳过不支持它们的浏览器的客户端检查,并简单地告诉您的用户将是什么类型的文件公认。

回答by Bryn

I am getting this (same/similar) error when attempting to Open a Jquery UI Dialog : SCRIPT5007: Unable to get value of the property '_focusTabbable': object is null or undefined.

尝试打开 Jquery UI 对话框时出现此(相同/类似)错误:SCRIPT5007:无法获取属性“_focusTabbable”的值:对象为空或未定义。

The change that I made that appears to have caused this error...:
a) Change to Jquery 1.9.1
b) Change to Jquery UI 1.10.1
c) Added 'appendTo' option to the dialog invocation.

我所做的更改似乎导致了此错误...:
a) 更改为 Jquery 1.9.1
b) 更改为 Jquery UI 1.10.1
c) 向对话框调用添加了“appendTo”选项。

a) + b) > c)

a) + b) > c)

I needed to do (c) because I had added (15) jquery dialogs to an existing page that previously had some subform elements disposed as hide/show div elements. I wanted to gain the jquery dialog functionality including modality. Problem was that the subform elements were rewritten by jquery by appending to the document, outside of the elements, thus formfields in the dialogs were no longer included when the form is submitted.

我需要做 (c),因为我已经向现有页面添加了 (15) 个 jquery 对话框,该页面以前将一些子表单元素设置为隐藏/显示 div 元素。我想获得包括模态在内的 jquery 对话框功能。问题是子表单元素被 jquery 重写,通过附加到元素之外的文档,因此在提交表单时不再包含对话框中的表单域。

Anyway does anyone have a better solution than the above to this issue ?

无论如何,有没有人有比上述更好的解决方案来解决这个问题?