【发布时间】:2021-12-30 23:22:36
【问题描述】:
document.querySelector("#myBtn").onclick = function () {
console.log("Clicked");
const h1 = document.querySelector("#myH1");
const underline = document.createElement("u");
underline.innerText = "Hey bro.";
h1.innerText = "";
h1.append(underline);
const p = document.querySelector(".myP");
p.forEach((element) => (element.innerText = "Thank You!!!!"));
const btn = document.querySelector("button");
btn.remove();
};
p.forEach((element) => (element.innerText = "Thank You!!!!"));
我得到了:Error TypeError p.forEach is not a function
【问题讨论】:
-
querySelector返回一个 HTML 元素,而不是类似结构的数组... -
使用
document.querySelectorAll(".myP")。 -
您的意思是使用
querySelectorAll?querySelector只返回第一个匹配节点(如果存在),而不是节点列表。 -
能否也提供 HTML 代码?
-
这能回答你的问题吗? forEach is not function
标签: javascript