This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
IE OnlyThis property is IE specific. Although it is well supported by IE, most other browsers no longer support this property. This property should only be used as one of the solutions when you need to be compatible with lower versions of IE, rather than relying on it completely in cross browser scripts.
object represents a fragment of text in a document, similar to the standard defined
This object is used to represent a specific piece of text in the document. For example, when you hold down the mouse to select the content on the page, you create a typical
. It is implemented in IE, then a
object can be created by
Returns the height of the rectangle bound to the
TextRangeobject and the left edge that completely contains the
TextRangeobject and the top edge that completely contains the
Returns the width of the rectangle bound to the
Returns a copy of
"word"means that the words at both ends of the range will completely included in the range.
xpand to wormay become
expand to words, etc.
move("character",-1)means to move one character to the left.
Booleanindicating whether the specified command can be executed successfully with the
execCommandmethod in the current state of the given document. You can also see
Booleanindicating the current state of the specified command. You can also see
DOMStringindicating the current value of the specified command. You can also see
Element.scrollIntoViewin the lower version of IE.
The following example is valid under IE9. The example gets the
, and sets the specified element to be selected. IE9 supports the standard alternative
var range = document.selection.createRange(); var element = document.getElementById("test"); range.moveToElementText(element); range.select(); // Selected "SomeTextToBeSelected"
<!DOCTYPE html> <html> <head> <title>TextRange Example</title> </head> <body> <p id="test">SomeTextToBeSelected
</p> </body> </html>
Valid only under
interface should be used first, if the browser allows it.
property returns a non-standard
object, and the
method creates a
object consistent with the currently selection.
var sel = document.selection; var range = sel.createRange(); alert(range.text); // Output plaintext of the selection
method does not create a reference. If you want to select the modified range after modifying the selection, you need to call the
object is the primary purpose of
. This object is used to process the selected ranges in the document, and is mainly implemented by using the
interface. According to the standard, a window / document may have multiple non adjacent selection, but only Firefox can select multiple ranges through
; generally, only one selected
is allowed in ie.
However, in other browsers,
does not have a so-called
attribute - they operate on the selection through the standard
, that is, they get the
object through the
method, and use the standard
object to process the text fragment. IE9 and later also gave up the
object and switched to the standard interface (although
has been reserved, it has lost its function in most cases).
It's easy to get confused. Generally, if the script only needs to be compatible with the latest browser, the standard interface is the best choice; however, the current website still wants to be compatible with IE8 or below browsers. Therefore, the best way is to deal with both at the same time, that is, try to use
mode when the standard interface is not supported, but do not regard it as the only choice.
||≤8(Standard API after IE9)||No Support（See Selection API ）|