无法迭代从Sizzle返回的数组

时间:2010-08-01 17:05:28

标签: javascript sizzle

根据我的理解,Sizzle返回一个对象数组(DOMElements),我试图在for循环中遍历那个对象数组但是我遇到了错误。当我尝试使用

获取属性时
obj[index-number]["property"]

它工作正常,但当我尝试将其传递给另一个函数后访问它

obj[index-number][arguments[index-number]]

我收到了未定义的回复。我尝试了很多不同的方法,包括解析点符号的eval无济于事。我很难过。任何指针或想法都会很棒。此外,我已经验证了函数的所有输入都是正确的(通过警告它们),同样,硬编码值以获得我想要的功能。这是我的代码:(对不起,它很冗长).....

var ecmafw = function() {
    // Creates the new instance of the object.

    // Sets up the objects global properties:
        this.error = false;

    // Checks to see if arguments were supplied, if none are then it returns false.
    if (arguments.lenght == 0) {
        this.error = "No arguments were supplied.";
        return false;
    }

    // Gives a reference to the result set.
    this.results = Sizzle(arguments[0]);

    this.attr = function() {
        /* Purpose: To add/remove/update an attribute from the results set.
         *
         * Can be used in two ways:
         *      1: .attr("attribute1='value' attribute2='value' attribute3='value'") // adds/removes them all. [negate value to be removed with "-" (used for class)]
         *      2: .attr("attribute", "value") // adds the one. [negate value to be removed with "-" (used for class)]
         *      3: .attr("attribute") // removes the one.
         *      4: .attr("attribute1 attribute2 attribute3") // removes them all.
        */ 

        var len = this.results.length;
        switch (arguments.length) {
            case 1:
                for (var a=0; a < len; a++) {
                    var re = new RegExp("=", "g");
                    if (re.test(arguments[0])) {
                        // Provided a list of attributes to update/create.
                        valuePairs = arguments[0].split("' ");

                        for (var i=0; i < valuePairs.length; i++) {
                            var attributeValue = valuePairs[i].split("=");
                            var newRE = new RegExp(/^-/);
                            var value = attributeValue[1].replace(/'/g, "");

                            if (newRE.test(value)) {
                                this.removeAttr(attributeValue[0], a, value);
                            } else {
                                this.setAttr(attributeValue[0], value, a);
                            }
                        }
                    } else {
                        var attributeSplit = arguments[0].split(" ");
                        if (attributeSplit.length == 1) {
                            // Provided a single attributes to remove.
                            this.removeAttr(arguments[0], a);
                        } else {
                            // Provided multiple attributes to remove.
                            for (var i=0; i < attributeSplit.length; i++) {
                                this.removeAttr(attributeSplit[i], a);
                            }
                        }
                    }
                }
                break;
            case 2:
                // Provided a single name/value pair to update.
                for (var a=0; a < len; a++) {
                    this.setAttr(arguments[0], arguments[1], a)
                }
                break;
            default:
                // Either 0 or more than 2 arguments were supplied.
                this.error = "There were no arguments supplied with the attr() function, or there were too many supplied.";
                return false
                break;
        }
    };

    this.setAttr = function() {
        // Counters for IE className
        if (document.all && !window.opera) {
            arguments[0] = arguments[0].replace(/class/gi, "className");
        }
        if (arguments[0] == "class" || arguments[0] == "className") {
            if (this.results[arguments[2]][arguments[0]] != undefined) {
                arguments[1] += " " + this.results[arguments[2]][arguments[0]]; // Failing
            }
        }
        if (this.results[arguments[2]].setAttribute) {
            this.results[arguments[2]].setAttribute(arguments[0], arguments[1]);
        } else {
            this.results[arguments[2]][arguments[0]] = arguments[1];
        }
    };

    this.removeAttr = function() {
        arguments[0] = arguments[0].replace(/class/gi, "className");
        var item = this.results[arguments[1]];

        if (arguments[0] == "className") {
            arguments[2] = arguments[2].replace("-", "");
            var replaceRE = new RegExp(arguments[2], "gi");

            // For some reason it would find it like this, This is fine but it is not working
            // in Opera. Opera is failing to convert item[eachItem] to an object. (so it says in its error log)
            for (var eachItem in item) { 
                if (arguments[0] == eachItem) {
                    item[eachItem]  = item[eachItem].replace(replaceRE, " ");
                    item[eachItem]  = item[eachItem].replace(/  /gi, " ");
                    item[eachItem]  = item[eachItem].replace(/^ /gi, "");
                    item[eachItem]  = item[eachItem].replace(/ $/gi, "");
                }
            }
        } else {
            if (this.results[arguments[1]].removeAttribute) {
                this.results[arguments[1]].removeAttribute(arguments[0]);
            } else {
                this.results[arguments[1]][arguments[0]] = "";
            }
        }
    };

    // Returns a reference to itself.
    return this;
}

1 个答案:

答案 0 :(得分:0)

不确定这可能是问题所在,但在removeAttr函数中,您正在访问此行传入的第3个参数:

arguments[2] = arguments[2].replace("-", "");

但是,在3次调用此函数中的2次中,您只传入2个参数。如果以上任何一种情况都会运行上述行arguments[2] undefined并且调用replace("-", "")会导致错误。

此外,您在初始参数附近检查错误:arguments.lenght