Document: phương thức createTextNode()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Tạo một nút Text mới. Phương thức này có thể được sử dụng để escape các ký tự HTML.
Cú pháp
js
createTextNode(data)
Tham số
data-
Một chuỗi chứa dữ liệu cần được đặt vào nút văn bản.
Giá trị trả về
Một nút Text.
Ví dụ
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} `);
});
});
Đặc tả kỹ thuật
| Specification |
|---|
| DOM> # ref-for-dom-document-createtextnode①> |