双击后,可以在 onclick 事件上正确获取选择范围,但是当我再次单击所选文本时,窗口选择应返回更新的选择范围,但这并没有发生。任何人都可以告诉我这是 javascript 选择中的错误还是他们已经这样做了。除了计时器之外,获得更新范围的解决方案是什么。
<div id="xyz" contenteditable="true">Hello world</div>
<span class="status">Selected text : </span>
Run Code Online (Sandbox Code Playgroud)
javascript代码:
function yourFunction() {
if (window.getSelection) {
var selectionRange = window.getSelection();
$('.status').text(selectionRange.toString());
}
}
$('#xyz').click(function () {
$('.status').text('Mouse click');
yourFunction();
})
Run Code Online (Sandbox Code Playgroud)
示例在这里