【问题标题】:Can't iterate array returned from Sizzle无法迭代从 Sizzle 返回的数组
【发布时间】:2010-08-02 03:38:01
【问题描述】:

据我了解,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;
}

【问题讨论】:

  • 什么浏览器出现错误?有些属性在 IE 中是只读的,所以如果你尝试设置它们,你会在 IE 中得到一个错误。

标签: javascript sizzle


【解决方案1】:

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

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

但是,在对这个函数的 3 次调用中,有 2 次只传入了 2 个参数。如果上面的行在其中任何一种情况下运行,arguments[2] 将是 undefined 并在其上调用 replace("-", "") 会引发错误。

此外,您在顶部附近的初始参数检查中有错字:arguments.lenght

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2019-02-12
    • 2021-12-26
    • 2019-12-24
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多