【发布时间】:2021-09-26 15:02:36
【问题描述】:
const box = document.querySelectorAll(".box");
console.log(box.length);
const direction = [
"top-start",
"bottom-start",
"left-start",
"right-start",
"top-center",
"bottom-center",
"left-center",
"right-center",
"top-end",
"bottom-end",
"left-end",
"right-end"
];
box.forEach((el, index) => {
for (let i = 0; i < direction.length; i++) {
CreateTooltip(el, direction[index], "Hello, World!");
}
});
上面提到的代码在DOM 中渲染144 工具提示,我只想要12 个,每个应该有不同的方向。我不知道为什么这个循环不起作用!我尝试在for 循环中添加forEach 循环,但问题仍然相同。
注意正如你们中的一些人所问的那样,我粘贴了我的整个代码。希望它会帮助你,然后你会帮助我。 ????
【问题讨论】:
-
嗯,您正在为每个调用创建并附加一个新的 DIV 元素,您希望得到什么?它将是 144,因为您有 12 个
.box元素和 12 个方向。 -
@Pezhvak 我该如何解决这个问题?我不是 JavaScript 专家!
-
box.forEach((element, index) => { CreateTooltip(element, direction[index], 'Hello, World!'); })使用此代码创建工具提示 -
@Flames 非常感谢!!有效。如果您想要任何项目的工具提示,请记住我!
标签: javascript arrays for-loop iteration