javascript - IE8 返回 'Object expected' 原型(prototype)

标签 javascript internet-explorer-8

我有这段代码,它适用于所有浏览器和 IE9>,但在 IE8 中它会抛出我 JScript Object Expected 第一行 (Array.prototype...)

this.items = Array.prototype.slice.call( document.querySelectorAll( '#' + this.el.id + ' > li' ) );
            this.itemsCount = this.items.length;
            this.itemsRenderedCount = 0;
            this.didScroll = false;

我试着改变 document.querySelectorAll( '#' + this.el.id + ' > li' )$(this).attr("id") - 同样的错误。

但是当我将此行更改为简单的 jquery picker 时,例如 $("#someId") ,它工作正常。

有人有想法吗?

最佳答案

这是因为 IE8 及更低版本需要一个 native 对象作为内置数组方法的 this 值。您传递的集合是一个宿主对象。

您需要手动进行转换。

这是一个简单的例子:

function _slice(arr) {
    try {   // try using .slice()
        return Array.prototype.slice.call(arr);
    } catch(e) {
            // otherwise, manually create the array
        var result = [];
        for (var i = 0; i < arr.length; ++i)
            result.push(arr[i]);
        return result;
    }
}

this.items = _slice( document.querySelectorAll( '#' + this.el.id + ' > li' ) );

旁注,因为 this.el 必须是 ulol 元素,所以它的子元素必须是 li 元素,为什么不这样做呢?

this.items = _slice( this.el.children );

关于javascript - IE8 返回 'Object expected' 原型(prototype),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20977832/

相关文章:

javascript - 根据嵌套键值对对象数组进行排序的最快方法

javascript - Rails 页面加载模态背景两次?

javascript - 如何在 fetch catch 中返回脱机文件的内容?

html - 图像出现在 p 背景之上,但在 IE8 中出现在 p 文本之后

Javascript:未触发 mousemove 事件处理程序

javascript - 我应该在javascript中使用 "this"还是 "event.target"?

html - <th> 将不会在 IE8 的 quirks 模式下与表格的其余部分一起水平滚动

html - 如何完全删除 IE8 的表格边框

css - IE8 的样式问题

javascript - 用于 JavaScript 生成的 URL 的 Python Web 爬虫