文件:createTextNode() 方法
建立一個新的 Text 節點。此方法可用於轉義 HTML 字元。
語法
js
createTextNode(data)
引數
data-
包含要放入文字節點的資料的字串。
返回值
一個 Text 節點。
示例
html
<button>YES!</button>
<button>NO!</button>
<button>WE CAN!</button>
<hr />
<p id="p1">First line of paragraph.</p>
js
function addTextNode(text) {
const newText = document.createTextNode(text);
const p1 = document.getElementById("p1");
p1.appendChild(newText);
}
document.querySelectorAll("button").forEach((button) => {
button.addEventListener("click", (event) => {
addTextNode(`${event.target.textContent} `);
});
});
規範
| 規範 |
|---|
| DOM # ref-for-dom-document-createtextnode① |
瀏覽器相容性
載入中…