这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

setBaseAndExtent() 方法在 Selection interface sets the selection to be a range including all or parts of two specified DOM nodes, and any content located between them.

句法

sel.setBaseAndExtent(anchorNode,anchorOffset,focusNode,focusOffset)
					

参数

anchorNode

The node at the start of the selection.

anchorOffset

The number of child nodes from the start of the anchor node that should be excluded from the selection. So for example, if the value is 0 the whole node is included. If the value is 1, the whole node minus the first child node is included. And so on.

focusNode

The node at the end of the selection.

focusOffset

The number of child nodes from the start of the focus node that should be included in the selection. So for example, if the value is 0 the whole node is excluded. If the value is 1, the first child node is included. And so on.

注意 : If the focus position appears before the anchor position in the document, the direction of the selection is reversed — the caret is placed at the beginning of the text rather the end, which matters for any keyboard command that might follow. For example, Shift + ➡︎ would cause the selection to narrow from the beginning rather than grow at the end.

返回值

Void.

异常

anchorOffset is larger than the number of child nodes inside anchorNode ,或者若 focusOffset is larger than the number of child nodes inside focusNode IndexSizeError exception is thrown.

范例

In this example, we have two paragraphs containing spans, each one containing a single word. The first one is set as the anchorNode and the second is set as the focusNode . We also have an additional paragraph that sits in between the two nodes.

Next, we have two form inputs that allow you to set the anchorOffset and focusOffset — they both have a default value of 0.

We also have a button that when pressed invokes a function that runs the setBaseAndExtent() method with the specified offsets, and copies the selection into the output paragraph at the very bottom of the HTML.

<h1>setBaseAndExtent example</h1>
<div>
  <p class="one"><span>Fish</span><span>Dog</span><span>Cat</span><span>Bird</span></p>
  <p>MIDDLE</p>
  <p class="two"><span>Car</span><span>Bike</span><span>Boat</span><span>Plane</span></p>
</div>
<div>
  <p>
    <label for="aOffset">Anchor offset</label>
    <input id="aOffset" name="aOffset" type="number" value="0">
  </p>
  <p>
    <label for="fOffset">Focus offset</label>
    <input id="fOffset" name="fOffset" type="number" value="0">
  </p>
  <p><button>Capture selection</button></p>
</div>
<p><strong>Output</strong>: <span class="output"></span></p>
					

The JavaScript looks like so:

var one = document.querySelector('.one');
var two = document.querySelector('.two');
var aOffset = document.getElementById('aOffset');
var fOffset = document.getElementById('fOffset');
var button = document.querySelector('button');
var output = document.querySelector('.output');
var selection;
button.onclick = function() {
  try {
    selection = document.getSelection();
    selection.setBaseAndExtent(one, aOffset.value, two, fOffset.value);
    var text = selection.toString();
    output.textContent = text;
  } catch(e) {
    output.textContent = e.message;
  }
}
					

Play with the live example below, setting different offset values to see how this affects the selection.

注意 : You can find this example on GitHub ( see it live also )。

规范

规范 状态 注释
选定 API
The definition of 'Selection.setBaseAndExtent()' in that specification.
工作草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
setBaseAndExtent Chrome Yes Edge 12 Firefox 53 IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 53 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: