La proprietà di sola lettura Node.nodeType è un numero intero che identifica il nodo. Distingue tra diversi tipi di nodi tra loro, come elements, text and comments.
Sintassi
var type = node.nodeType;
Restituisce un numero intero che specifica il tipo del nodo. I valori possibili sono elencati in Tipi di nodi costanti.
Constanti
Tipi di nodi costanti
| Constante | Valore | Descrizione |
|---|---|---|
Node.ELEMENT_NODE |
1 |
Un nodo Element come <p> o <div>. |
Node.TEXT_NODE |
3 |
L'attuale Text dentro un Element o Attr. |
Node.CDATA_SECTION_NODE |
4 |
Una CDATASection, ad esempio <!CDATA[[ … ]]>. |
Node.PROCESSING_INSTRUCTION_NODE |
7 |
Una ProcessingInstruction di un documento XML, come <?xml-stylesheet … ?>. |
Node.COMMENT_NODE |
8 |
Un nodo Comment, come <!-- … -->. |
Node.DOCUMENT_NODE |
9 |
Un nodo Document. |
Node.DOCUMENT_TYPE_NODE |
10 |
Un nodo DocumentType, come <!DOCTYPE html>. |
Node.DOCUMENT_FRAGMENT_NODE |
11 |
Un nodo DocumentFragment. |
Tipi di nodo deprecati
Le seguenti costanti sono state deprecate e non dovrebbero essere più utilizzate.
| Constante | Valore | Descrizione |
Node.ATTRIBUTE_NODE |
2 | Un Attribute di un Element. Gli attributi non implementano più l'interfaccia Node dal DOM4. |
Node.ENTITY_REFERENCE_NODE |
5 | Un nodo di riferimento di entità XML, come &foo;. Rimosso nel DOM4. |
Node.ENTITY_NODE |
6 | Un nodo XML <!ENTITY …>. Rimosso nel DOM4. |
Node.NOTATION_NODE |
12 | Un nodo XML <!NOTATION …>. Rimosso nel DOM4. |
Esempi
Diversi tipi di nodi
document.nodeType === Node.DOCUMENT_NODE; // true
document.doctype.nodeType === Node.DOCUMENT_TYPE_NODE; // true
document.createDocumentFragment().nodeType === Node.DOCUMENT_FRAGMENT_NODE; // true
var p = document.createElement("p");
p.textContent = "Once upon a time…";
p.nodeType === Node.ELEMENT_NODE; // true
p.firstChild.nodeType === Node.TEXT_NODE; // true
Commenti
Questo esempio controlla se il primo nodo all'interno dell'elemento del documento è un commento e visualizza un messaggio in caso contrario.
var node = document.documentElement.firstChild;
if (node.nodeType !== Node.COMMENT_NODE) {
console.warn("Dovresti commentare il tuo codice!");
}
Specifiche
| Specifica | Stato | Commento |
|---|---|---|
| DOM The definition of 'Node.nodeType' in that specification. |
Living Standard | Deprecated ATTRIBUTE_NODE, ENTITY_REFERENCE_NODE and NOTATION_NODE types. |
| Document Object Model (DOM) Level 3 Core Specification The definition of 'Node.nodeType' in that specification. |
Obsolete | Nessun cambiamento. |
| Document Object Model (DOM) Level 2 Core Specification The definition of 'Node.nodeType' in that specification. |
Obsolete | Nessun cambiamento. |
| Document Object Model (DOM) Level 1 Specification The definition of 'Node.nodeType' in that specification. |
Obsolete | Definizione iniziale. |
Compatibilità con i browser
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
nodeType | Chrome Full support Yes | Edge Full support 12 | Firefox Full support Yes | IE ? | Opera Full support Yes | Safari Full support Yes | WebView Android Full support Yes | Chrome Android Full support Yes | Firefox Android Full support Yes | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- Compatibility unknown
- Compatibility unknown