Node: thuộc tính nodeName
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.
Thuộc tính chỉ đọc nodeName của Node trả về tên của nút hiện tại dưới dạng một chuỗi.
Giá trị
Một chuỗi. Các giá trị cho những kiểu nút khác nhau là:
Attr-
Giá trị của
Attr.name, tức qualified name của thuộc tính. CDATASection-
Chuỗi
"#cdata-section". Comment-
Chuỗi
"#comment". Document-
Chuỗi
"#document". DocumentFragment-
Chuỗi
"#document-fragment". DocumentType-
Giá trị của
DocumentType.name Element-
Giá trị của
Element.tagName, tức tên viết hoa của thẻ phần tử nếu là phần tử HTML, hoặc tên thẻ viết thường nếu là phần tử XML (như phần tử SVG hoặc MathML). ProcessingInstruction-
Giá trị của
ProcessingInstruction.target Text-
Chuỗi
"#text".
Ví dụ
Ví dụ này hiển thị tên nút của một số nút
html
Đây là một số HTML:
<div id="d1">Hello world</div>
<!-- Ví dụ về chú thích -->
Text <span>Text</span> Text<br />
<svg height="20" width="20">
<circle cx="10" cy="10" r="5" stroke="black" stroke-width="1" fill="red" />
</svg>
<hr />
<output id="result">Chưa được tính.</output>
và đoạn script sau:
js
let node = document.querySelector("body").firstChild;
let result = "Tên các nút là:\n";
while (node) {
result += `${node.nodeName}\n`;
node = node.nextSibling;
}
const output = document.getElementById("result");
output.innerText = result;
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| DOM> # ref-for-dom-node-nodename①> |