javascript – 专注于嵌套的contenteditable元素

所以,我有两个嵌套在另一个内部的contenteditable div:

<div class="top" contenteditable="true">
     <div class="nested" contenteditable="true">This</div>
</div>

这是Fiddle.

当它被聚焦时,应该集中嵌套,但是使用console.log(document.activeElement);它显示顶部是聚焦的,它不识别嵌套的div.

在正在编辑内容的情况下,我需要识别嵌套的div元素而不是顶部元素.

我怎么做到这一点?任何帮助都感激不尽.

解决方法

[contenteditable]元素由浏览器处理的方式使任何嵌套的[contenteditable]不处理任何事件,编辑主机是以前可编辑的父级.见 spec

If an element is editable and its parent element is not,or if an
element is editable and it has no parent element,then the element is
an editing host. Editable elements can be nested. User agents must
make editing hosts focusable (which typically means they enter the tab
order). An editing host can contain non-editable sections,these are
handled as described below. An editing host can contain non-editable sections that contain further editing hosts.

现在作为一种解决方法,您可以通过将其任何可编辑的父级临时设置为不可编辑来使托管主机具有集中嵌套的可编辑元素.见例如:

$('div.top [contenteditable]').on('focusin focusout',function(e) {
    $(this).parents('[contenteditable]').prop('contenteditable',e.type === "focusout");
});

-updated jsFiddle-

相关文章

前言 做过web项目开发的人对layer弹层组件肯定不陌生,作为l...
前言 前端表单校验是过滤无效数据、假数据、有毒数据的第一步...
前言 图片上传是web项目常见的需求,我基于之前的博客的代码...
前言 导出Excel文件这个功能,通常都是在后端实现返回前端一...
前言 众所周知,js是单线程的,从上往下,从左往右依次执行,...
前言 项目开发中,我们可能会碰到这样的需求:select标签,禁...