HTML:
<p>
<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui aliquam quod aperiam veniam animi. Debitis iure sit incidunt sint dicta enim voluptatum inventore itaque cumque error. Neque voluptatem beatae fuga?</span>
<span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui aliquam quod aperiam veniam animi. Debitis iure sit incidunt sint dicta enim voluptatum inventore itaque cumque error. Neque voluptatem beatae fuga?</span>
</p>
<button>Remove range</button>
JavaScript的:
var p = document.body.firstElementChild;
var rng, selectText;
p.addEventListener('mouseup', function() {
rng = document.createRange();
rng.selectNodeContents(p);
selectText = window.getSelection();
selectText.addRange(rng);
}, false);
document.body.lastElementChild.addEventListener('click', function() {
selectText.removeRange(rng);
}, false);
我无法理解为什么该方法不起作用Selection.removeRange()?我们的想法是只保留原始选择。