非标
此特征是非标准的,且不在标准轨道中。不要在面向 Web 的生产站点中使用它:它不适用于每个用户。实现之间可能存在大的不兼容性,且行为将来可能改变。
IE Only
This 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.
A
TextRange
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
TextRange
. It is implemented in IE, then a
TextRange
object can be created by
Element.createTextRange()
or
Document.selection.createRange()
.
Note that this interface is not supported in non IE browsers. Alternative
Selection
and
范围
interfaces can be used.
TextRange.boundingHeight
只读
Returns the height of the rectangle bound to the
TextRange
对象。
TextRange.boundingLeft
只读
TextRange
object and the left edge that completely contains the
TextRange
对象。
TextRange.boundingTop
只读
TextRange
object and the top edge that completely contains the
TextRange
对象。
TextRange.boundingWidth
只读
Returns the width of the rectangle bound to the
TextRange
对象。
TextRange.htmlText
TextRange
.
TextRange.text
TextRange
.
TextRange.collapse()
Move the caret to the beginning or end of the current range.
TextRange.duplicate()
Returns a copy of
TextRange
.
TextRange.execCommand()
TextRange.expand()
"word"
means that the words at both ends of the range will completely included in the range.
xpand to wor
may become
expand to words
,等。
TextRange.findText()
Searches the specified text in the original range and adjusts the range to include the first match.
TextRange.inRange()
Returns whether the current range contains the specified range.
TextRange.isEqual()
Returns whether the current range is equal to the specified range.
TextRange.move()
move("character",-1)
means to move one character to the left.
TextRange.moveEnd()
Moves the end of the range by a specified number of units.
TextRange.moveStart()
Moves the start of the range by a specified number of units.
TextRange.moveToElementText()
元素
对象。
TextRange.parentElement()
Returns the parent element of the range, which is the smallest element that contains the range completely. If the selection contains more than one element, when you modify the contents of the selection, the contents will be placed in the corresponding position of the parent element instead of the child element.
TextRange.pasteHTML()
Paste the HTML content into the given range and replace any previous text and HTML elements in the range.
TextRange.queryCommandEnabled()
布尔
indicating whether the specified command can be executed successfully with the
execCommand
method in the current state of the given document. You can also see
Document.queryCommandEnabled()
.
TextRange.queryCommandState()
布尔
indicating the current state of the specified command. You can also see
Document.queryCommandState()
.
TextRange.queryCommandValue()
DOMString
indicating the current value of the specified command. You can also see
Document.queryCommandValue()
.
TextRange.scrollIntoView()
Element.scrollIntoView
in the lower version of IE.
TextRange.select()
Select the current range (i.e. the blue selection seen by the user).
TextRange.setEndPoint()
TextRange
.
The following example is valid under IE9. The example gets the
TextRange
透过
document.selection
, 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
IE9
。
selection
interface should be used first, if the browser allows it.
document.selection
property returns a non-standard
selection
object, and the
selection.createRange()
method creates a
TextRange
object consistent with the currently selection.
var sel = document.selection; var range = sel.createRange(); alert(range.text); // Output plaintext of the selection
注意,
createRange
method does not create a reference. If you want to select the modified range after modifying the selection, you need to call the
TextRange.select
方法。
selection
兼容性
document.selection
object is the primary purpose of
TextRange
. This object is used to process the selected ranges in the document, and is mainly implemented by using the
TextRange
interface. According to the standard, a window / document may have multiple non adjacent selection, but only Firefox can select multiple ranges through
Ctrl
; generally, only one selected
TextRange
is allowed in ie.
However, in other browsers,
document
does not have a so-called
selection
attribute - they operate on the selection through the standard
选定 API
, that is, they get the
Selection
object through the
window.getselection()
method, and use the standard
范围
object to process the text fragment. IE9 and later also gave up the
document.selection
object and switched to the standard interface (although
TextRange
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
TextRange
mode when the standard interface is not supported, but do not regard it as the only choice.
| IE | Other Browsers | |
|---|---|---|
TextRange
|
≤8(Standard API after IE9) | No Support(See 选定 API ) |
TextRange
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
CustomEvent
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument