无法迭代从 Sizzle 返回的数组

发布于 2024-09-12 08:04:42 字数 5557 浏览 3 评论 0原文

根据我的理解,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;
}

From my understanding Sizzle returns an array of objects (DOMElements), I am trying to walk that array of objects in a for loop but I am getting errors. When I try to get a property with the

obj[index-number]["property"]

it works fine, but when I try to access it after passing it to another function

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

I am getting a return of undefined. I have tried many different ways, including eval to parse the dot notation to no avail. I am stumped. Any pointers or ideas would be awesome. Also, I have verified all input to the function is correct (through alerting them out), also, hard coding the values to get what I want in the function works as well. Here is my code: (sorry it's lengthy).....

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2024-09-19 08:04:42

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

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

但是,在对此函数的 3 次调用中的 2 次中,您仅传入 2 个参数。如果上面的行在这两种情况下运行,arguments[2] 将是 undefined 并调用 replace("-", "")它会抛出一个错误。

此外,您在顶部附近的初始参数检查中存在拼写错误:arguments.lenght

Not sure if this might be the problem, but in the removeAttr function you are accessing the 3rd argument passed in on this line:

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

However, in 2 of the 3 calls to this function you only pass in 2 arguments. If the above line runs in either of those cases arguments[2] would be undefined and calling replace("-", "") on it would throw an error.

Also, you have a typo in your initial arguments check near the top: arguments.lenght.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文