简体   繁体   English

document.getSelection 在选择中带有回车

[英]document.getSelection with carriage return in selection

I've tried many different things and am at a loss.我尝试了很多不同的东西,但不知所措。 The code below illustrates the issue.下面的代码说明了这个问题。 I have an editable element.我有一个可编辑的元素。

If I select a paragraph of text, the Selection.anchorNode is a #text node.如果我选择一段文本,则 Selection.anchorNode 是一个 #text 节点。

If when selecting a paragraph I include a carriage return preceding the paragraph, Selection.anchorNode is instead the containing span element.如果在选择段落时我在段落前包含一个回车符,则 Selection.anchorNode 是包含的 span 元素。

What I need to know is what the offset is from the start of the span element's innerText value.我需要知道的是从 span 元素的 innerText 值开始的偏移量是多少。 When a carriage return IS NOT included in the selection, I am able simply to analyze the sibling nodes of the anchorNode.当返回返回不包括在选择中时,我能够简单地分析anchornode的兄弟节点。 But when a carriage return IS included in the selection, I don't seem to have the information to achieve this.但是,当在选择中包含回程返回时,我似乎没有这些信息来实现这一目标。

Any guidance as to what I am missing would be appreciated.任何关于我遗漏的指导将不胜感激。

 function showResult() { let sel = document.getSelection(); document.getElementById("output").textContent ="document.getSelection().anchorNode.nodeName: " + sel.anchorNode.nodeName; } document.getElementById("textContainer").innerText = "This is the first paragraph\\n\\nSelecting this paragraph with and without the preceding carriage return yields very different anchorNodes";
 #textContainer { position: relative; display: inline-block; width: 400px; height: 100px; border: 1px solid steelblue; margin: 5px; } #output { position: relative; width: 400px; height: 100px; border: 1px solid steelblue; margin: 5px; }
 <div> <span id="textContainer" contenteditable="true"></span> </div> <input type="button" onclick="showResult()" value="Write selection object to console" /> <div id="output"> </div>

It seems browser return parent node while selecting carriage return so you can use if condition for this situation:似乎浏览器在选择回车时返回父节点,因此您可以在这种情况下使用if条件:

sel.anchorNode.hasChildNodes()?sel.anchorNode.childNodes[0].nodeName:sel.anchorNode.nodeName

 function showResult() { let sel = document.getSelection(); document.getElementById("output").textContent ="document.getSelection().anchorNode.nodeName: " + (sel.anchorNode.hasChildNodes()?sel.anchorNode.childNodes[0].nodeName:sel.anchorNode.nodeName); } document.getElementById("textContainer").innerText = "This is the first paragraph\\n\\nSelecting this paragraph with and without the preceding carriage return yields very different anchorNodes";
 #textContainer { position: relative; display: inline-block; width: 400px; height: 100px; border: 1px solid steelblue; margin: 5px; } #output { position: relative; width: 400px; height: 100px; border: 1px solid steelblue; margin: 5px; }
 <div> <span id="textContainer" contenteditable="true"></span> </div> <input type="button" onclick="showResult()" value="Write selection object to console" /> <div id="output"> </div>

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

相关问题 从document.getSelection()中获取多个元素 - Get multiple elements from document.getSelection() 将document.getSelection()扩展到整个段落 - Expand document.getSelection() to entire paragraph 使用Sinon创建document.getSelection的存根 - Creating a stub of document.getSelection using Sinon 在 textarea 上调用焦点会破坏 document.getSelection() - Calling focus on a textarea breaks document.getSelection() document.getSelection() 和 window.getSelection() 之间的区别 - Diff between document.getSelection() and window.getSelection() Javascript document.all和document.getSelection-Firefox替代 - Javascript document.all and document.getSelection - Firefox alternative document.getSelection 返回的对象中的 anchorNode 、 baseNode 、 extentNode 和 focusNode 是什么? - What is anchorNode , baseNode , extentNode and focusNode in the object returned by document.getSelection? document.getSelection()。toString()在使用iframe时即始终为“” - document.getSelection().toString() is always “ ” in ie while working with iframe Range = document.getSelection().getRangeAt(0) 在 Safari 上不起作用 - Range = document.getSelection().getRangeAt(0) doesn't work on Safari 如何检测 document.getSelection().modify('extend', 'forward', 'word') 是否到达文档末尾? - How to detect if document.getSelection().modify('extend', 'forward', 'word') got to the end of document?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM