XML DOM - Objeto Range
- Página anterior DOM ProcessingInstr
- Próxima página DOM RangeException
O objeto Range representa uma área contínua no documento.
Objeto Range
O objeto Range representa uma área contínua do documento, como a área selecionada pelo usuário ao arrastar com o mouse na janela do navegador.
Se uma implementação suportar o módulo Range, então Objeto Documentdefiniu Método createRange(),chamando-o você pode criar novos objetos Range.
Note:O IE definiu o método Document.createRange() incompatível, o objeto retornado é semelhante ao interface Range, mas incompatível.
O interface Range define muitos métodos para especificar a área selecionada do documento, além de alguns métodos que podem ser usados para operações de cortar e colar na área selecionada.
Os atributos do interface Range fornecem métodos para obter os nós de limite e o deslocamento do intervalo. Seus métodos fornecem métodos para definir os limites do intervalo. Note que os limites do intervalo podem ser definidos como Documento ou Objeto DocumentFragmentdos nós. Assim que os pontos de limite do intervalo forem definidos, métodos como deleteContents(), extractContents(), cloneContents() e insertNode() podem ser usados para realizar operações de cortar, copiar e colar.
Quando a alteração do documento através de operações de inserção ou exclusão, todos os objetos Range que representam parte do documento mudarão (se necessário), para manter seus pontos de limite válidos e para que eles (o mais próximo possível) representem o mesmo conteúdo do documento.
Término: intervalo, ponto de limite e deslocamento
Um intervalo tem dois pontos de limite, um ponto de início e um ponto de fim. Cada ponto de limite é especificado por um nó e o deslocamento desse nó. O nó geralmente é Nó Elemento、Nó Documentoou Nó TextoPara Element e Documento nós, o deslocamento se refere aos filhos do nó. O deslocamento é 0, significa que o ponto de limite está antes do primeiro filho do nó. O deslocamento é 1, significa que o ponto de limite está após o primeiro filho do nó, antes do segundo filho. Mas se o nó de limite for um nó Texto, o deslocamento se refere à posição entre dois caracteres do texto.
Constants of the Range object
These constants specify how to compare the boundary points of Range objects.
They are the compareBoundaryPoints() method how Valid values for parameters:
Constants | Description |
---|---|
START_TO_START | Compare the starting point of the specified range with the starting point of the current range. |
START_TO_END | Compare the starting point of the specified range with the ending point of the current range. |
END_TO_END | Compare the ending point of the specified range with the ending point of the current range. |
END_TO_START | Compare the ending point of the specified range with the starting point of the current range. |
Properties of the Range object
Note:All properties are read-only and cannot be changed by setting these properties to change the starting and ending points of the range. You must call the setEnd() method and the setStart() method to do this.
Note:Any read operation on these properties after calling the detach() method will throw an error with the code INVALID_STATE_ERR Exceção DOMException.
Properties | Description |
---|---|
collapsed | If the starting and ending points of the range are at the same location in the document, it is true, i.e., the range is empty or collapsed. |
commonAncestorContainer | The common ancestor container of the starting and ending points of the range (i.e., their ancestor nodes), the deepest nested Document node. |
endContainer | The Document node that contains the ending point of the range. |
endOffset | The ending point position in the endContainer. |
startContainer | The Document node that contains the starting point of the range. |
startOffset | The starting point position in the startContainer. |
Methods of the Range object
Note:If the detach() method of the range has been called, any subsequent call to a method of the Range object will throw an error with the code INVALID_STATE_ERR Exceção DOMException.
Method | Description |
---|---|
cloneContents() | Returns a new DocumentFragment object that contains a copy of the document region represented by the range. |
cloneRange() | Create a new Range object that represents the same document region as the current Range object. |
collapse() | Folding the range so that its boundary points coincide. |
compareBoundaryPoints() | Compara os pontos de limite da faixa especificada e os pontos de limite da faixa atual, retornando -1, 0 e 1 com base na ordem. O ponto de limite a comparar é especificado pelo primeiro parâmetro, que deve ser um dos constantes definidos anteriormente. |
deleteContents() | Remove a área de documento representada pelo objeto Range atual. |
detach() | Notifica a implementação de que o escopo atual não está mais em uso e pode parar de rastrear. Se esse método da faixa for chamado, qualquer chamada subsequente a qualquer método da faixa lançará uma exceção com o código INVALID_STATE_ERR. Exceção DOMException. |
extractContents() | Remove a área de documento representada pela faixa atual e retorna o conteúdo daquela área na forma de um objeto DocumentFragment. Este método, juntamente com o cloneContents() e o deleteContents(), são semelhantes. |
insertNode() | Insere o nó especificado no ponto de início da faixa do documento. |
selectNode() | Define os pontos de limite da faixa para incluir o nó especificado e todos seus descendentes. |
selectNodeContents() | Define os pontos de limite da faixa para incluir os descendentes do nó especificado, mas não o nó em si. |
setEnd() | Define o ponto de fim da faixa para o nó especificado e o deslocamento. |
setEndAfter() | Define o ponto de fim da faixa para imediatamente após o nó especificado. |
setEndBefore() | Define o ponto de fim da faixa para imediatamente antes do nó especificado. |
setStart() | Define o ponto de início da faixa para o deslocamento especificado dentro do nó. |
setStartAfter() | Define o ponto de início da faixa para imediatamente após o nó especificado. |
setStartBefore() | Define o ponto de início da faixa para imediatamente antes do nó especificado. |
surroundContents() | Insere o nó especificado no ponto de início da faixa do documento, e redefine o pai de todos os nós na faixa para torná-los descendentes do nó inserido. |
toString() | Retorna o conteúdo de texto puro da área de documento representada pela faixa. |
- Página anterior DOM ProcessingInstr
- Próxima página DOM RangeException