我有个变数 topBarText . 该变量是一个包含文本链接的字符串 "<a href="index.html">Link</a>" 变量声明为 托巴特文 document.body.appendChild(topBarText); 给出以下错误:
topBarText
"<a href="index.html">Link</a>"
托巴特文
document.body.appendChild(topBarText);
Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
我需要做什么才能将此变量追加到页面中?
你需要把节点变成那个字符串。
const a = document.createElement('a'); // is a node a.href = 'index.html'; a.innerHTML = 'Link'; document.body.appendChild(a);