非标
此特征是非标准的,且不在标准轨道中。不要在面向 Web 的生产站点中使用它:它不适用于每个用户。实现之间可能存在大的不兼容性,且行为将来可能改变。
Selection.modify()
method applies a change to the current selection or cursor position, using simple textual commands.
sel.modify(alter, direction, granularity)
alter
"move"
to move the current cursor position or
"extend"
to extend the current selection.
direction
"forward"
or
"backward"
to adjust in the appropriate direction based on the language at the selection point. If you want to adjust in a specific direction, you can specify
"left"
or
"right"
.
granularity
"character"
,
"word"
,
"sentence"
,
"line"
,
"paragraph"
,
"lineboundary"
,
"sentenceboundary"
,
"paragraphboundary"
,或
"documentboundary"
.
"sentence"
,
"paragraph"
,
"sentenceboundary"
,
"paragraphboundary"
,或
"documentboundary"
. WebKit and Blink do.
注意:
Starting in
Gecko 5.0
,
"word"
granularity no longer includes the following space, regardless of the default platform behavior. This makes the behavior more consistent, as well as making it work the same way WebKit used to work, but unfortunately they have recently changed their behavior.
This example demonstrates the various
granularity
options for modifying a selection. Click somewhere inside the example (optionally selecting some text), and then click the button to expand the selection.
<p>Click somewhere in this example. Then click the button below to expand the selection. Watch what happens!</p> <p>Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.</p> <label for="granularity">Granularity:</label> <select id="granularity"> <option value="character">Character</option> <option value="word">Word</option> <option value="sentence">Sentence</option> <option value="line">Line</option> <option value="paragraph">Paragraph</option> <option value="lineboundary">Line Boundary</option> <option value="sentenceboundary">Sentence Boundary</option> <option value="paragraphboundary">Paragraph Boundary</option> <option value="documentboundary">Document Boundary</option> </select> <br><br> <button>Extend selection</button>
let select = document.querySelector('select');
let button = document.querySelector('button');
button.addEventListener('click', modify);
function modify() {
let selection = window.getSelection();
selection.modify('extend', 'forward', select.value);
}
This method is not part of any specification.
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
修改
非标
|
Chrome Yes | Edge ≤79 | Firefox 4 | IE ? | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 4 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
node
parameter is nullable
|
Chrome Yes | Edge ≤79 | Firefox 55 | IE ? | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 55 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
offset
parameter is optional
|
Chrome Yes | Edge ≤79 | Firefox 55 | IE ? | Opera Yes | Safari Yes | WebView Android Yes | Chrome Android Yes | Firefox Android 55 | Opera Android Yes | Safari iOS Yes | Samsung Internet Android Yes |
完整支持
兼容性未知
实验。期望将来行为有所改变。
非标。预期跨浏览器支持较差。
Selection
, the interface it belongs to.
Selection
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