[英]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.