Esta página foi traduzida do inglês pela comunidade. Saiba mais e junte-se à comunidade MDN Web Docs.
NodeFilter
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since julho de 2015.
Uma interface NodeFilter representa um objeto usado para filtrar os nós (elementos), em uma iteração NodeIterator ou TreeWalker. Essas interfaces não conhecem nada sobre o DOM ou sobre como percorrer ou atravessar, elementos; elas apenas sabem como avaliar se um único nó atende aos requisitos do filtro fornecido ou não.
Nota:
O navegador não fornece nenhum objeto que implemente essa interface. É esperado que o desenvolvedor escreva tal objeto, utilizando o método acceptNode() conforme as suas necessidades, podendo mesclar com objetos do tipo TreeWalker ou NodeIterator em sua implementação..
Properties
Essa interface não implementa, nem herda, nenhuma propriedade.
Methods
Essa interface não herda nenhum método.
NodeFilter.acceptNode()-
Returns an
unsigned shortthat will be used to tell if a givenNodemust be accepted or not by theNodeIteratororTreeWalkeriteration algorithm. This method is expected to be written by the user of aNodeFilter. Possible return values are:Constant Description FILTER_ACCEPTValue returned by the NodeFilter.acceptNode()method when a node should be accepted.FILTER_REJECTValue to be returned by the NodeFilter.acceptNode()method when a node should be rejected. ForTreeWalker, child nodes are also rejected. ForNodeIterator, this flag is synonymous with FILTER_SKIP.FILTER_SKIPValue to be returned by NodeFilter.acceptNode()for nodes to be skipped by theNodeIteratororTreeWalkerobject. The children of skipped nodes are still considered. This is treated as "skip this node but not its children".
Example
var nodeIterator = document.createNodeIterator(
// Node to use as root
document.getElementById("someId"),
// Only consider nodes that are text nodes (nodeType 3)
NodeFilter.SHOW_TEXT,
// Object containing the function to use for the acceptNode method
// of the NodeFilter
{
acceptNode: function (node) {
// Logic to determine whether to accept, reject or skip node
// In this case, only accept nodes that have content
// other than whitespace
if (!/^\s*$/.test(node.data)) {
return NodeFilter.FILTER_ACCEPT;
}
},
},
false,
);
// Show the content of every non-empty text node that is a child of root
var node;
while ((node = nodeIterator.nextNode())) {
alert(node.data);
}
Specifications
| Specification |
|---|
| DOM> # dom-document-createnodeiterator> |
Compatibilidade com navegadores
Loading...
See also
- Related interfaces:
TreeWalker,NodeIterator.