【问题标题】:how to get jquery.couch.app.js to work with IE8如何让 jquery.couch.app.js 与 IE8 一起工作
【发布时间】:2010-04-28 03:47:46
【问题描述】:

我已经在 IE7 和 IE8(在所有兼容模式下)的 Windows XP SP3 和 IE8(在所有兼容模式下)的 Windows 7 Ultimate 上对此进行了测试,并且在两者上都以相同的方式失败。我正在运行来自 couchapp 存储库的最新 HEAD。这在我的 OSX 10.6.3 开发机器上运行良好。我已经在 Windows 7 Ultimate 上使用 Chrome 4.1.249.1064 (45376) 和 Firefox 3.6 进行了测试,它们都可以正常工作。 OSX 10.6.3 上的 Safari 4 和 Firefox 3.6 也是如此

这是错误信息

网页错误详情

用户代理:Mozilla/4.0(兼容; MSIE 8.0;视窗 NT 6.1;三叉戟/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0) 时间戳:4 月 28 日,星期三 2010 年 03:32:55 UTC

消息:对象不支持这个 属性或方法行:159 字符:7 代码:0 URI: http://192.168.0.105:5984/test/_design/test/vendor/couchapp/jquery.couch.app.js

这是“有问题”的代码,它在 Chrome、Firefox 和 Safari 上运行良好。如果说失败出现在从文件jquery.couch.app.js 开始的qs.forEach() 行上

  157 var qs = document.location.search.replace(/^\?/,'').split('&');
  158 var q = {};
  159 qs.forEach(function(param) {
  160   var ps = param.split('=');
  161   var k = decodeURIComponent(ps[0]);
  162   var v = decodeURIComponent(ps[1]);
  163    if (["startkey", "endkey", "key"].indexOf(k) != -1) {
  164     q[k] = JSON.parse(v);
  165   } else {
  166    q[k] = v;
  167   }
  168 });

【问题讨论】:

  • 在 IE 中不起作用...现在我在哪里听说过...?

标签: javascript jquery internet-explorer couchapp


【解决方案1】:

forEach() 是最近添加到 JavaScript 规范中的函数,因此并非所有浏览器都支持它。

您可以在 MDC 上了解它: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/forEach

在“兼容性”下,您会找到一个使 forEach() 可用的 sn-p。

if (!Array.prototype.forEach)
{
  Array.prototype.forEach = function(fun /*, thisp*/)
  {
    var len = this.length >>> 0;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
        fun.call(thisp, this[i], i, this);
    }
  };
}

因此,将上面的代码复制并粘贴到您的脚本中,forEach() 应该可以工作了。

【讨论】:

  • 这只是部分解决了我的问题,因为我必须添加indexOf,正如我对这个问题的回答中提到的那样。
【解决方案2】:

在修复 forEach() 问题后,我还必须将 indexOf() 添加到 Array 对象以使其正常工作

if (!Array.prototype.indexOf)
{
    Array.prototype.indexOf = function(elt)
    {
        var len = this.length >>> 0;

        var from = Number(arguments[1]) || 0;
        from = (from < 0)
                ? Math.ceil(from)
                : Math.floor(from);
        if (from < 0)
            from += len;

        for (; from < len; from++)
        {
            if (from in this &&
                this[from] === elt)
                return from;
        }
        return -1;
    };
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-02
    • 2017-06-25
    • 2020-12-13
    • 2012-02-02
    • 2011-02-22
    • 2013-03-24
    • 2016-07-22
    • 2011-08-17
    相关资源
    最近更新 更多