5

当我在 Mozilla 中运行我的程序时,它会解析剔除表达式并显示可观察数组中的值。当我在 IE7 中执行相同操作时,它会显示敲除代码。

Mozilla 结果

value 1
value 2
value 3

IE7 结果

function observable() { 
   if (arguments.length > 0) {
        // Write
        // Ignore writes if the value hasn't changed
        if ((!observable['equalityComparer']) || !observable['equalityComparer'](_latestValue, arguments[0])) {
            observable.valueWillMutate();
            _latestValue = arguments[0];
            if (DEBUG) observable._latestValue = _latestValue;
            observable.valueHasMutated();            
        }
        return this; // Permits chained assignments        
   }        
   else {
        // Read
        ko.dependencyDetection.registerDependency(observable); // The caller only needs to be notified of changes if they did a "read" operation
        return _latestValue;       
   }
}

我怎样才能使它在 IE7 中正常工作?

4

1 回答 1

2

IE 浏览器不支持数组的 indexOf,这会导致 knockout.js 框架出现问题。

添加以下javascript,它可能会解决您的问题:

    //
    // IE browsers do not support indexOf method for an Array. Hence 
    // we add it below after performing the check on the existence of
    // the same.
    //
    if (!Array.prototype.indexOf)
    {
        Array.prototype.indexOf = function (obj, start)
        {
            for (var i = (start || 0), j = this.length; i < j; i++)
            {
                if (this[i] === obj)
                {
                    return i;
                }
            }
            return -1;
        };
    }
于 2012-12-01T05:18:00.493 回答