gpt4 book ai didi

javascript - 无法迭代从 Sizzle 返回的数组

转载 作者:行者123 更新时间:2023-11-30 06:48:44 25 4
gpt4 key购买 nike

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

最佳答案

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

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

但是,在对该函数的 3 次调用中的 2 次中,您只传递了 2 个参数。如果以上行在任何一种情况下运行,arguments[2] 将是 undefined 并调用 replace("-", "")它会抛出一个错误。

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

关于javascript - 无法迭代从 Sizzle 返回的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3382925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com