--- title: Document.caretRangeFromPoint() slug: Web/API/Document/caretRangeFromPoint translation_of: Web/API/Document/caretRangeFromPoint ---

{{APIRef("DOM")}}{{Non-standard_header}} 

{{domxref("Document")}} 的 caretRangeFromPoint() 方法返回一个 Range 对象(指定坐标的文档片段)。

语法

var range = document.caretRangeFromPoint(float x, float y);

返回

其中一项:

参数

x
当前视图的横向位置。
y
当前视图的纵向位置。

示例

Basic demo: 点击文档,在点击的位置插入一行。

HTML Content

<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,
sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>

JavaScript Content

function insertBreakAtPoint(e) {

    var range;
    var textNode;
    var offset;

    if (document.caretPositionFromPoint) {
        range = document.caretPositionFromPoint(e.clientX, e.clientY);
        textNode = range.offsetNode;
        offset = range.offset;

    } else if (document.caretRangeFromPoint) {
        range = document.caretRangeFromPoint(e.clientX, e.clientY);
        textNode = range.startContainer;
        offset = range.startOffset;
    }

    // only split TEXT_NODEs
    if (textNode && textNode.nodeType == 3) {
        var replacement = textNode.splitText(offset);
        var br = document.createElement('br');
        textNode.parentNode.insertBefore(br, replacement);
    }
}

var paragraphs = document.getElementsByTagName("p");
for (i=0 ; i < paragraphs.length; i++) {
    paragraphs[i].addEventListener("click", insertBreakAtPoint, false);
}

{{ EmbedLiveSample('Example', '', '', '', 'Web/API/Document/caretRangeFromPoint') }}

 

浏览器兼容性

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Edge Opera Safari (WebKit)
Basic support {{CompatChrome(43.0)}} 20+ {{CompatNo()}} 12 15+ {{CompatVersionUnknown}}
Feature Android Chrome for Android Edge Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatNo}} {{CompatChrome(43.0)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}