简体   繁体   English

IE8返回“对象预期”原型

[英]IE8 returns 'Object expected' prototype

I have this code, that works good on all browser and IE9> , but in IE8 it throws me JScript Object Expected on the first line ( Array.prototype... ) 我有这段代码,在所有浏览器和IE9>上都可以正常工作,但是在IE8中,它使我在第一行( Array.prototype... )上看到了JScript Object Expected

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

I tried changing document.querySelectorAll( '#' + this.el.id + ' > li' ) to $(this).attr("id") - same error. 我尝试将document.querySelectorAll( '#' + this.el.id + ' > li' )更改$(this).attr("id") -相同的错误。

But when i change this line to simple jquery picker , for example $("#someId") , it works fine. 但是,当我将此行更改为简单的jquery选择器时,例如$("#someId") ,它可以正常工作。

Ideas anyone? 有任何想法吗?

This is because IE8 and lower requires a native object as the this value of built-in Array methods. 这是因为IE8和更低版本需要内置对象作为内置Array方法的this值。 The collection you're passing is a host object. 您传递的集合是一个宿主对象。

You need to do the conversion manually instead. 您需要手动进行转换。

Here's a simple example: 这是一个简单的例子:

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' ) );

Side note, since this.el must be a ul or ol element, and so its children must be li elements, why not just do this? 旁注,由于this.el必须是ulol元素,因此其子元素必须是li元素,为什么不这样做呢?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM