我剛開始學習 Javascript,我正在嘗試在 li 標簽內附加錨標簽。
const li = document.createElement("li")
const Anchor = document.createElement("a")
Anchor.href = "index.html"
li.appendChild(Anchor)
我就是這樣做的。但是當我運行這個時,
“在 'Node' 上執行 'appendChild' 失敗:'a' 型別的節點不能插入 'LI' 型別的節點內。”
這個錯誤出來了。我該如何解決?
uj5u.com熱心網友回復:
首先你需要create一個元素 ( a)
const li = document.createElement("li")
const Anchor = document.createElement("a")
// SET attribute
// Anchor.href = "index.html"
// OR
const linkHref = document.createAttribute("href");
linkHref.value = "index.html";
Anchor.setAttributeNode(linkHref);
li.appendChild(Anchor)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/472636.html
標籤:javascript html
上一篇:Python日期時間格式
