--- 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);
其中一项:
Null
, 若 x 或 y 不存在、在视图外或在没有文本节点的位置。Basic demo: 点击文档,在点击的位置插入一行。
<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>
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') }}
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}} |