简体   繁体   English

获取 cursor 插入符号所在的选定 contenteditable 元素的子元素的 ID

[英]Get ID of selected contenteditable element's child where cursor caret is located

I'm currently trying to make an editor using contenteditable divs, but I'm having an issue where clicking backspace in say child-2 at the start will result in merging child-1 and child-2 together, which defeats its own purpose.我目前正在尝试使用 contenteditable div 制作编辑器,但我遇到了一个问题,即在开始时单击child-2中的退格键会导致child-1child-2合并在一起,这违背了它自己的目的。

I'm finding the current caret position using the function:我正在使用 function 找到当前的插入符号 position:

caret: function() {
    if (window.getSelection) {
        sel = window.getSelection();
        if (sel.getRangeAt) return sel.getRangeAt(0).startOffset;
    }
    return null;
}

Which has been working perfectly, but to fix the merging issue I need to find out which element is currently selected and use that data with the caret location to use event.preventDefault() and stop a potential merge.哪个工作得很好,但是要解决合并问题,我需要找出当前选择了哪个元素,并将该数据与插入符号位置一起使用,以使用event.preventDefault()并停止潜在的合并。

This is frame that I'm using and talking about:这是我正在使用和谈论的框架:

<div id="parent" contenteditable="true">
  <div id="child-1">
    One
  </div>
  <div id="child-2">
    Two
  </div>
  <div id="child-3">
    Three
  </div>
</div>

To find the selected element I've tried this:要找到我尝试过的选定元素:

console.log(document.activeElement);

To see if this prints out the ID of the child selected, though this outputs the entire parent element into the console instead of just the ID.要查看这是否会打印出所选子项的 ID ,尽管这会将整个父元素输出到控制台中,而不仅仅是 ID。

Use Event Delegation to easily find the clicked node. 使用事件委托可以轻松找到单击的节点。 Other events such as key and mouse can be added as well. 也可以添加其他事件,例如按键和鼠标。

Details are commented in demo 演示中有详细评论

Demo 演示

 // Refernce the parent of all of the target nodes var parent = document.getElementById('parent'); // Register the click event to #parent parent.addEventListener('click', idNode); // This is the callback that is invoked on each click function idNode(e) { /* If the node clicked (e.target) is not the || the registered event listener || (e.currentTarget = #parent) */ if (e.target !== e.currentTarget) { // Get the #id of clicked node var ID = e.target.id; // Reference e.target by its #id var child = document.getElementById(ID); } // Log the #id of each e.target at every click console.log('The caret is located at ' + ID); // Return the e.target as a DOM node when needed return child; } 
 <div id="parent" contenteditable="true"> <div id="child-1"> One </div> <div id="child-2"> Two </div> <div id="child-3"> Three </div> </div> 

You can apply tabindex="0" to the child elements, which makes them focusable, which will select the children when using document.activeElement (otherwise it's always the parent which has the focus): 您可以将tabindex="0"应用于子元素,从而使它们可聚焦,这将在使用document.activeElement时选择子元素(否则,始终是具有焦点的父元素):

 console.log(document.activeElement); 
 <div id="parent" contenteditable="true"> <div id="child-1" tabindex="0"> One </div> <div id="child-2" tabindex="0"> Two </div> <div id="child-3" tabindex="0"> Three </div> </div> 

After comment: I am adding a screenshot of this very snippet: I clicked into the word "Two": You can see the dotted border around that line indicating the focus status: 发表评论后:我正在添加此片段的屏幕截图:单击单词“ Two”:您可以看到该行周围的虚线边框指示焦点状态:

在此处输入图片说明

By using document.getSelection().getRangeAt(0).commonAncestorContainer.parentNode.parentNode.parentNode.id you can get the id of the parent element of the caret.通过使用document.getSelection().getRangeAt(0).commonAncestorContainer.parentNode.parentNode.parentNode.id您可以获得插入符号的父元素的 id。

Example例子

 function getParentId() { var caret = document.getSelection().getRangeAt(0); var caretParent = caret.commonAncestorContainer.parentNode; console.log(caretParent.id); }
 <div id="parent" contenteditable="true"> <div id="child-1" tabindex="0"> One </div> <div id="child-2" tabindex="0"> Two </div> <div id="child-3" tabindex="0"> Three </div> </div> <button onclick="getParentId()">Get Id</button>

暂无
暂无

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

相关问题 根据可编辑的Div中的插入符位置获取所选元素 - Get Selected Element based on caret position in contenteditable Div Javascript - 在[contenteditable]的子元素中获取插入符号后面的元素 - Javascript - Get element behind caret in child elements of [contenteditable] 获取多个可编辑的div中的最新插入符号位置,并将图像附加到最后出现光标的div中 - Get the latest caret position among multiple contenteditable div and append image to the div where the cursor was present lastly contentEditable游标的父元素 - contentEditable cursor's parent element 获取一个元素的子节点索引,该元素的插入符号位置到它的 contenteditable 父元素 - Get the child node index of an element having the caret position to its contenteditable parent element 将插入符号(光标)设置在可疑段落中的粗体元素内 - setting caret (cursor) inside bold element present inside contenteditable paragraph 将光标(插入符号)放置在特定位置 <pre> 内容可编辑元素 - Place cursor(caret) in specific position in <pre> contenteditable element 如何在 contenteditable 元素(div)中设置插入符号(光标)position? - How to set the caret (cursor) position in a contenteditable element (div)? 如何在contentEditable元素的子元素中设置光标? - How to set the cursor in the child of a contentEditable element? 获取包含HTML内容的contentEditable区域中的插入符(光标)位置 - Get caret (cursor) position in contentEditable area containing HTML content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM