简体   繁体   English

Javascript - 在[contenteditable]的子元素中获取插入符号后面的元素

[英]Javascript - Get element behind caret in child elements of [contenteditable]

I am building a simplistic and easy-to-use text editor in Javascript, basically a WYSIWYG editor. 我正在使用Javascript构建一个简单易用的文本编辑器,基本上是一个WYSIWYG编辑器。 I will be using the contenteditable attribute to the main wrapper ( .wrapper ). 我将使用contenteditable属性到主包装器( .wrapper )。 When you press enter inside the .wrapper , a new <p> element with a unique id gets appended to the wrapper. 当您在.wrapper按Enter .wrapper ,具有唯一id的新<p>元素将附加到包装器。

I need a way to fetch which child element of the .wrapper that is currently selected (ie, being focused or having the caret/text marker inside of it). 我需要一种方法来获取当前被选中的.wrapper哪个子元素(即,被聚焦或在其中具有插入/文本标记)。

I've searched for days without any results, and I've tried using document.elementFromPoint() but without any proper results. 我搜索了几天没有任何结果,我尝试使用document.elementFromPoint()但没有任何正确的结果。

When using $(":focus") , I get the entire .wrapper and not the specific child element. 当使用$(":focus") ,我得到整个.wrapper而不是特定的子元素。

Edit: 编辑:

Example HTML structure: 示例HTML结构:

<div class="container t-wrapper" contenteditable>
</div>

Example Javascript code: 示例Javascript代码:

$(document).ready(function() {
    $currentElement = $("[contenteditable]");

    $(".t-wrapper").each(function() {
        var id = generateIdentifier(6); // Ignore this

        /* This creates the initial <p> child element, where you start typing. */
        $(this).html('<p class="t-empty t-first" id="' + id + '"></p>');

        $(this).on("mouseup", function() {
            /* $currentElement = whatever element the caret is inside. */
        });

        $(this).on("keyup", function() {
            /* $currentElement = whatever element the caret is inside. */
        });
    ));
});

Edit 2: 编辑2:

I managed to get it fairly working with the mouseup event, since you're actually clicking on something. 我设法让它完全适用于mouseup事件,因为你实际上是在点击某些东西。 But I need this to work when moving the caret using the keyboard . 但是在使用键盘移动插入符号时我需要这个。 Alternatively, I need some way to get the position of the caret in pixels, and then use document.elementFromPoint() to get the specific element. 或者,我需要一些方法来获取插入符号的位置(以像素为单位),然后使用document.elementFromPoint()来获取特定元素。

:focus doesn't select your elements because they are not focusable. :focus不会选择您的元素,因为它们不可:focus

You can make them focusable by adding tabindex="-1" in HTML, or tabIndex = -1 in JS. 你可以通过在HTML中添加tabindex="-1"或在JS中添加tabIndex = -1来使它们具有可聚焦性。

 var generateIdentifier = Math.random; var currentElement = document.querySelector("[contenteditable]"); [].forEach.call(document.querySelectorAll(".t-wrapper"), function(el) { var first = document.createElement('p'); first.className = "t-empty t-first"; first.id = generateIdentifier(6); first.textContent = 'Press enter to create new paragraphs'; first.tabIndex = -1; el.appendChild(first); }); 
 .container > :focus { border: 1px solid blue; } 
 <div class="container t-wrapper" contenteditable></div> 

It seems that if you add it to the first paragraph, new paragraphs obtain it automatically. 似乎如果将它添加到第一段,新段落会自动获取它。 But if you want to be sure, I guess you could use a mutation observer or a keyup event listener to detect paragraphs without tabindex , and add it: 但是如果你想确定,我猜你可以使用变异观察者或keyup事件监听器来检测没有tabindex段落,并添加它:

el.addEventListener("keyup", function(e) {
  var newChild = el.querySelector('p:not([tabindex])');
  if(newChild) newChild.tabIndex = -1;
});

document.activeElement将返回当前关注的元素。

暂无
暂无

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

相关问题 如何使用 html 子元素在 contenteditable div 中获取插入符号 position? - How to get caret position within contenteditable div with html child elements? 使用contentEditable时,如何使用Javascript获取插入符号元素? - How can I get the element the caret is in with Javascript, when using contentEditable? javascript:将插入符号移动到元素的结尾(div与contenteditable) - javascript: move caret to end of element (div with contenteditable) 获取一个元素的子节点索引,该元素的插入符号位置到它的 contenteditable 父元素 - Get the child node index of an element having the caret position to its contenteditable parent element 如何在Internet Explorer中使用html子元素在contenteditable div中获取插入位置 - How to get caret position within contenteditable div with html child elements in Internet Explorer 获取 cursor 插入符号所在的选定 contenteditable 元素的子元素的 ID - Get ID of selected contenteditable element's child where cursor caret is located Javascript:获取内容中的文本可编辑到插入符号 - Javascript: Get text in a contenteditable up to the caret 获取内容Editable caret position - Get contentEditable caret position 如何获取 ContentEditable 元素中关于innerText 的插入符号位置? - How to get the caret position in ContentEditable elements with respect to the innerText? 根据可编辑的Div中的插入符位置获取所选元素 - Get Selected Element based on caret position in contenteditable Div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM