简体   繁体   English

在绝对位置设置从A到B的选择范围

[英]Set a selection range from A to B in absolute position

How can I select programmatically from A(x1,y1) to B(x2,y2) ? 如何从A(x1,y1)到B(x2,y2)以编程方式选择?

x1, y1, x2, y2 are pixel coordinates. x1,y1,x2,y2是像素坐标。 I searched a lot and in all functions I found, we had to specify a specific tag and then it selects its content. 我搜索了很多,在我发现的所有功能中,我们必须指定一个特定的标签,然后选择它的内容。

You can do this in current versions of all browsers. 您可以在所有浏览器的当前版本中执行此操作。 These browsers have at least one of the following: 这些浏览器至少具有以下之一:

  • The standards-based approach, implemented by Firefox >= 20, from the CSSOM View spec: document.caretPositionFromPoint() 基于标准的方法,由Firefox> = 20实现,来自CSSOM View规范: document.caretPositionFromPoint()
  • WebKit's proprietary version of the same: document.caretRangeFromPoint() . WebKit的专有版本相同: document.caretRangeFromPoint()
  • IE's proprietary TextRange object, which has a moveToPoint() method that takes pixel coordinates. IE专有的TextRange对象,它有一个采用像素坐标的moveToPoint()方法。 However, it seems that moveToPoint() , which is used in all version of IE, can be buggy (see here and here , for example); 但是,看起来在所有版本的IE中使用的moveToPoint()可能都是错误的(例如,见这里这里 ); I've simply been lucky that has worked in all the documents I've used it in. 我很幸运,曾经使用过我用过的所有文件。

However, Mozilla does not yet implement any of these and neither does Opera, so this can't be done in those browsers yet. 但是,Mozilla尚未实现其中任何一项,Opera也没有实现,因此在这些浏览器中尚无法实现。

Firefox 20 and later supports document.caretPositionFromPoint() . Firefox 20及更高版本支持document.caretPositionFromPoint() Opera 15 supports document.caretRangeFromPoint() Opera 15支持document.caretRangeFromPoint()

Here's some example code. 这是一些示例代码。 It works in IE 5+, WebKit from around 2010 onwards, Firefox >= 20 and Opera >= 15. 它适用于IE 5 +,WebKit从2010年左右开始,Firefox> = 20,Opera> = 15。

Live demo: http://jsfiddle.net/timdown/ABjQP/ 现场演示: http//jsfiddle.net/timdown/ABjQP/

Code: 码:

function createSelectionFromPoint(startX, startY, endX, endY) {
    var doc = document;
    var start, end, range = null;
    if (typeof doc.caretPositionFromPoint != "undefined") {
        start = doc.caretPositionFromPoint(startX, startY);
        end = doc.caretPositionFromPoint(endX, endY);
        range = doc.createRange();
        range.setStart(start.offsetNode, start.offset);
        range.setEnd(end.offsetNode, end.offset);
    } else if (typeof doc.caretRangeFromPoint != "undefined") {
        start = doc.caretRangeFromPoint(startX, startY);
        end = doc.caretRangeFromPoint(endX, endY);
        range = doc.createRange();
        range.setStart(start.startContainer, start.startOffset);
        range.setEnd(end.startContainer, end.startOffset);
    }
    if (range !== null && typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (typeof doc.body.createTextRange != "undefined") {
        range = doc.body.createTextRange();
        range.moveToPoint(startX, startY);
        var endRange = range.duplicate();
        endRange.moveToPoint(endX, endY);
        range.setEndPoint("EndToEnd", endRange);
        range.select();
    }
}

For Firefox or Opera there is a little workaround. 对于Firefox或Opera,有一些解决方法。 This set the caret at the first position of an element: 这将插入符号设置在元素的第一个位置:

console.log("moz or opera doesn't support caret by position so we have a workaround");
var range = doc.createRange();
var element = doc.elementFromPoint(startX, startY);
range.setStart(element, 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript:如何从Selection / Range对象开始获取绝对字符位置? - JavaScript: How to get absolute char position starting from Selection/Range objects? 在Javascript中按范围设置选择 - Set selection by range in Javascript javascript:从时间a的光标位置到时间b的光标位置创建范围 - javascript: create range from cursor position at time a to cursor position at time b 需要将光标位置设置为contentEditable div的末尾,与选择和范围对象一起发出问题 - Need to set cursor position to the end of a contentEditable div, issue with selection and range objects 将div设置为position:相对于position:absolute; 使用turn.js时 - set the div to position:relative from position:absolute; when using turn.js JavaScript 将鼠标位置转换为选择范围 - JavaScript convert mouse position to selection range Javscript - 显示没有绝对 position 的中音文本以处理文本选择 - Javscript - Display alto text without absolute position to handle selection on text 如何设置绝对位置div中心对齐 - how to set the absolute position div center align 无法设置模态窗口的绝对位置 - Unable to set the absolute position of modal window 如何在JavaScript中为位置绝对元素设置边界 - How to set boundaries for position absolute element in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM