我想选择<div>
中具有特定范围ex的文本。
<div>This is a sample text.</div>
selectText(3, 12);
它将在div中从s到m(s是sam)中选择文本,并且用户可以通过按 ctrl + c
复制文本答案 0 :(得分:2)
let element = document.querySelector('div');
selectText(element, 20, 35);
function selectText(node, start, end) {
let text = node.childNodes[0];
let range = new Range();
let selection = document.getSelection();
range.setStart(text, start);
range.setEnd(text, end);
selection.removeAllRanges();
selection.addRange(range);
}
<div>Text not to select. Text to select. Text not to select.</div>
答案 1 :(得分:-2)
slice(3,12);