属性'removeChild'在类型 'NodeListOf<Element>' 上不存在



我试图删除包含className的所有元素,但我没有得到"属性'remove'不存在类型'NodeListOf。"错误。

const questionKey = SelectElement.getAttribute("data-key").value;
const currentQuestionId = SelectElement.getAttribute("data-current-question").value;
const questionData = getCategoryQuestionsByProfile?.data?.questionOptionsList.filter(
(d) => Number(hierarchyQuestionId) === Number(d.id)
);
const bqCounter = document.querySelectorAll(".bq-counter");
// for (let index = 0; index < bqCounter.length; index++) {
//   if (index + 1 > questionLevel) {
//     let elem = document.querySelectorAll(".bq_detect_" + questionKey + "_" + (index + 1));
//     elem.remove();
//   }
// }
bqCounter.forEach((e, index){
if (index + 1 > questionLevel) {
let elem = document.querySelectorAll(".bq_detect_" + questionKey + "_" + (index + 1));
elem.remove();
}
} )

querySelectorAll返回一个NodeList,为了删除该列表中的所有元素,您应该迭代并删除元素,例如:

const elems = document.querySelectorAll(".bq_detect_" + questionKey + "_" + (index + 1));
for (const elem of elems) {
elem.remove();
}

如果只有一个元素具有这个类名,您可以简单地使用:

const elem = document.querySelector(".bq_detect_" + questionKey + "_" + (index + 1));
elem.remove();

相关内容

最新更新