Selection.modify()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.

非标准: 该特性是非标准的,请尽量不要在生产环境中使用它!

Selection.modify() 方法可以通过简单的文本命令来改变当前选区或光标位置。

语法

js
modify(alter, direction, granularity)

参数

alter

改变类型。传入 "move" 来移动光标位置,或者 "extend" 来扩展当前选区。

direction

调整选区的方向。你可以传入 "forward""backward" 来根据选区内容的语言书写方向来调整。或者使用 "left""right" 来指明一个明确的调整方向。

granularity

调整的距离颗粒度。可选值有 "character""word""sentence""line""paragraph""lineboundary""sentenceboundary""paragraphboundary""documentboundary"

备注: Gecko 不支持 "sentence", "paragraph", "sentenceboundary", "paragraphboundary""documentboundary". Webkit 和 Blink 支持。

备注: 从 Gecko 5.0 开始,不管是不是浏览器的默认行为,"word" 颗粒度不再包括单词后面的空格。这让这个行为变得更加稳定,这也和之前的 Webkit 保持一致,然而不幸的是他们最近修改了这个默认行为。

示例

使当前选区往语言书写方向扩大一个单词(word)的选择范围

js
var selection = window.getSelection();
selection.modify("extend", "forward", "word");

规范

无。

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
modify

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见