我正在开发一个书签应用程序,我必须在其中存储用户选择的关键字、单词或内容。我使用 createRange() 和 addRange() javascript 方法来创建范围,然后找出用户选择的元素/内容。我为此编写的代码如下。
<head>
<script type="text/javascript">
var storedSelections = [];
function StoreSelection () {
if (window.getSelection) {
var currSelection = window.getSelection ();
for (var i = 0; i < currSelection.rangeCount; i++) {
storedSelections.push (currSelection.getRangeAt (i));
}
currSelection.removeAllRanges ();
} else {
alert ("Your browser does not support this example!");
}
}
function ClearStoredSelections () {
storedSelections.splice (0, storedSelections.length);
}
function ShowStoredSelections () {
if (window.getSelection) {
var currSelection = window.getSelection ();
currSelection.removeAllRanges ();
for (var i = 0; i < storedSelections.length; i++) {
currSelection.addRange (storedSelections[i]);
}
} else {
alert ("Your browser does not support this example!");
}
}
</script>
</head>
<body>
Select some content on this page and use the buttons below.<br /> <br />
<button onclick="StoreSelection ();">Store the selection</button>
<button onclick="ClearStoredSelections ();">Clear stored selections
</button>
<button onclick="ShowStoredSelections ();">Show stored selections</button>
</body>
这段代码在 Firefox 上完美运行。我能够一一选择多个内容,并能够再次显示所选内容,但在 chrome 和 chromium 上我得到Discontiguous selection is not supported.
当我在范围数组中存储多个元素并单击“显示存储的选择”按钮时,出现错误。
帮助将不胜感激。如果有其他替代方法可以完成此书签任务,请建议我。
Write
window.getSelection().removeAllRanges();
就在创建范围之前。
https://bugs.chromium.org/p/chromium/issues/detail?id=399791
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)