IE只有getElementsByTagName的javascript错误 [英] IE only javascript error with getElementsByTagName

查看:101
本文介绍了IE只有getElementsByTagName的javascript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码适用于FF / Chrome

I have the following code which works in FF / Chrome

var stack = [Array.prototype.slice.call(document.getElementsByTagName("body")[0].childNodes)], nodes, node, parent, text, offset;
while (stack.length) {
    nodes = stack.pop();
    for (var i=0, n=nodes.length; i<n; ++i) {
        node = nodes[i];
        switch (node.nodeType) {
            case Node.ELEMENT_NODE:
                if (node.nodeName.toUpperCase() !== "SCRIPT") {
                    stack.push(Array.prototype.slice.call(node.childNodes));
                }
                break;
            case Node.TEXT_NODE:
                text = node.nodeValue;
                offset = text.indexOf("[test=");
                if (offset >= 0 && text.substr(offset).match(/^(\[test=(\d+)\])/)) {
                    parent = node.parentNode;
                    var before = document.createTextNode(text.substr(0, offset));
                        link = document.createElement("a"),
                        after = document.createTextNode(text.substr(offset + RegExp.$1.length));
                    link.appendChild(document.createTextNode(text.substr(offset, RegExp.$1.length)));
                    link.setAttribute("href", "http://example.com/" + RegExp.$2);
                    parent.insertBefore(after, node);
                    parent.insertBefore(link, after);
                    parent.insertBefore(before, link);
                    parent.removeChild(node);
                    stack.push([after]);
                }
        }
    }
}

基本上它的作用是如果它在页面中找到[test = 25]它将其转换为指向example.com/25的链接

Basically what it does is if it finds [test=25] in the page it converts it to a link which points to example.com/25

在IE中我得到以下内容错误:第一行预期的JScript对象:

In IE I get the following error: JScript Object Expected on first line:

var stack = [Array.prototype.slice.call(document.getElementsByTagName("body")[0].childNodes)], nodes, node, parent, text, offset;

IE7和IE8都会出现此错误。

This error occurs in both IE7 and IE8.

任何帮助将不胜感激。

谢谢。

推荐答案

NodeList 对象上调用 Array.prototype.slice 是不合法的> childNodes 属性(或其他各种DOM方法)。

It's not legal to call Array.prototype.slice on a NodeList object as returned by the childNodes property (or various other DOM methods).

通常,调用 Thing是不合法的。 prototype.method 除了一个 Thing 的实例之外,传统上允许的浏览器 - 以及ECMAScript第三版标准要求 - 对许多人来说是一个特例 Array.prototype 方法,以便可以在任何本机JavaScript对象上调用它们,就像 Array 一样。这意味着,它们可以在参数对象上使用,它看起来像数组但实际上并非如此t。

Normally it wouldn't be legal to call Thing.prototype.method on anything but an instance of Thing, however browsers have traditionally allowed — and the ECMAScript Third Edition standard requires — a special case for many Array.prototype methods so that they can be called on any native-JavaScript object which is sufficiently like an Array. This means, notably, that they can be used on the arguments object, which looks like an Array but actually isn't.

但是, NodeList 并且DOM中的其他集合对象未定义为本机JavaScript对象;它们被允许成为主机对象,它们完全由浏览器而不是语言实现。所有投注都是关闭主机对象...

However, NodeList and the other collection objects in the DOM are not defined to be native JavaScript objects; they are allowed to be ‘host objects’, which are implemented completely by the browser and not the language. All bets are off for host objects...


切片功能是否可以成功应用于主机对象是依赖于实现的。

Whether the slice function can be applied successfully to a host object is implementation-dependent.

所以 Array.prototype.slice 可能不适用于NodeList和IE在版本8之前,实际上它不会。

So Array.prototype.slice may not work for NodeList, and in IE before version 8, indeed, it won't.

如果你想制作NodeList的普通数组副本,你必须做很长时间但是安全方式:

If you want to make a plain-Array copy of a NodeList, you'll have to do it the long but safe way:

Array.fromSequence= function(seq) {
    var arr= new Array(seq.length);
    for (var i= seq.length; i-->0;)
        if (i in seq)
            arr[i]= seq[i];
    return arr;
};

var stack = [Array.fromSequence(document.body.childNodes)];

顺便提一下,你可以使用 textnode.splitText让链接器更简单一些,我对使用全局 RegExp 属性非常谨慎,好像任何意外的正则表达式工作都发生在其中一个干预调用中会迷路的。查看匹配对象通常更好。请参见此问题针对基本相同问题的另一次攻击。

Incidentally, you can make that linkifier a bit simpler by using textnode.splitText, and I'd be very wary about using the global RegExp properties, as if any unexpected regex work occurs in one of the intervening calls they'll be lost. Looking at the match object is usually better. See this question for another attack at basically the same problem.

这篇关于IE只有getElementsByTagName的javascript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆