【发布时间】:2022-11-02 00:10:20
【问题描述】:
仅限 JavaScript! 从下拉菜单中单击一个元素后,我试图获取索引。 我已经得到了文本值,但没有得到索引。我该如何解决这个问题?
1)这是我点击一个月后获取文本值的方式:
const monthText = document.querySelector(".month-text");
const months = document.querySelectorAll(".month-value");
const monthsEl = Array.from(months);
months.forEach((el) =\> {
el.onclick = function () {
const monthSelected = (monthText.textContent = this.innerHTML);
console.log(monthSelected);
};
});
2)我正在获取索引,但使用硬代码(“May”):
const monthsArr = monthsEl.map((el) => el.textContent);
console.log(monthsArr);
const index = monthsArr.indexOf("May");
console.log(index);
【问题讨论】:
-
欢迎来到 Stack Overflow!,请take the tour,并阅读how to ask、On Topic question,然后查看Question Check list、perfect question 以及如何创建Minimal, Complete and Verifiable Example - 请不要发布代码图像/控制台输出。在此处粘贴文本
标签: javascript