这是可能的,我之前已经发布了代码来从 Stack Overflow 上的鼠标事件创建一个范围:
https://stackoverflow.com/a/12924488/96100
这是一个允许使用鼠标右键进行选择的演示:
http://jsfiddle.net/timdown/BpjyG/6/
它包括来自链接答案的代码以及以下内容:
var anchorRange;
function mouseMove(evt) {
evt = evt || window.event;
var range = getMouseEventCaretRange(evt), sel, selRange;
if (typeof range.setEndPoint != "undefined") {
// Old IE
selRange = anchorRange.duplicate();
selRange.setEndPoint("EndToEnd", range);
if (selRange.text == "") {
// Try the other way round
selRange = anchorRange.duplicate();
selRange.setEndPoint("StartToStart", range);
}
selRange.select();
} else if (window.getSelection) {
sel = window.getSelection();
if (sel.extend) {
// This is ideal: we can create forwards and backwards selections
// simply by moving the focus using extend()
sel.extend(range.startContainer, range.startOffset);
} else {
// We'll need to create a range and test for backwardsness
selRange = document.createRange();
selRange.setStart(sel.anchorNode, sel.anchorOffset);
selRange.setEnd(range.startContainer, range.startOffset);
if (selRange.collapsed) {
// Try the other way roound
selRange.setStart(range.startContainer, range.startOffset);
selRange.setEnd(sel.anchorNode, sel.anchorOffset);
}
selectRange(selRange);
}
}
}
function mouseUp(evt) {
evt = evt || window.event;
if (evt.button == 2) {
if (document.releaseCapture) {
document.releaseCapture();
}
document.onmousemove = null;
document.onmouseup = null;
anchorRange = null;
}
}
document.oncontextmenu = function() {
return false;
};
document.onmousedown = function(evt) {
evt = evt || window.event;
if (evt.button == 2) {
rightMouseButtonDown = true;
var range = getMouseEventCaretRange(evt);
anchorRange = range;
selectRange(range);
if (document.body.setCapture) {
document.body.setCapture();
}
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;
return false;
}
};