简体   繁体   English

如何找到文本选择的顶级父元素?

[英]How to find the top parent element of a text selection?

I have many contenteditable div. 我有很多contenteditable div。

When I select a text by hightlighting inside a contenteditable div, I want to retrieve the contenteditable div element. 当我通过在contenteditable div内部突出显示来选择文本时,我想检索contenteditable div元素。

For exemple : 举个例子 :

<div contenteditable="true">

Hello World, 

<i>this is <b>a dog</b> in the garden</i>

Thank you very much

</div>

So when I select the text "dog" by highlighting, I want to retrieve the contenteditable div element in order to know which contenteditable div I have used. 因此,当我通过突出显示选择文本“ dog”时,我想检索contenteditable div元素以便知道我使用了哪个contenteditable div。

An idea ? 一个主意 ?

Edit : 编辑:

I made this code, but it's not perfect : 我编写了这段代码,但这并不完美:

var selection = window.getSelection();

    var node = selection.anchorNode;

    for(var i = 0; i < 50; i++)
    {
        node = node.parentNode;

        if(node.getAttribute("contenteditable") == "true")
        {
            console.log("found");
            break;  
        }
    }

I think previous answers are over-complicating matters. 我认为先前的答案过于复杂。

This is the same whether you select text or just click on an element. 无论您选择文本还是单击元素,都是一样的。

You need to be able to identify the current element that you have clicked on or pressed a key on (otherwise I don't see how you can make contenteditable work sensibly). 您需要能够识别您单击或按下某个键的当前元素(否则,我看不到如何明智地使内容可编辑的工作)。

Assuming you can do this, all you need to do is go back from CurrentElm to the parent that has the contenteditable attribute with a while loop - while Elm.contenteditable != true {Elm=Elm.parent} 假设您可以执行此操作,那么您要做的就是从CurrentElm返回到具有while循环的contenteditable属性的父级-而Elm.contenteditable!= true {Elm = Elm.parent}

I confess to not providing full programming details, but I can if you need them. 我承认没有提供完整的编程详细信息,但是如果您需要它们,我可以提供。

The first issue is to be able to identify the CurrentElm on mouseclick or keypress. 第一个问题是能够通过单击鼠标或按键识别CurrentElm。

If you can't do this, then get back to me and I'll explain - the rest then becomes easy. 如果您无法执行此操作,请与我联系,我会解释-其余的事情变得很容易。

You need an event to fire after the text is highlighted. 文本突出显示后,您需要触发一个事件。 Unfortunatley, there isn't a onhighlight event so you must use something like onmouseup . 不幸的是,这里没有onhighlight事件,因此您必须使用onmouseup东西。

Next, you need a way to add event listeners to all your contenteditable divs. 接下来,您需要一种将事件侦听器添加到所有contenteditable div的方法。 One way to use this is document.querySelectorAll() . 一种使用此方法的方法是document.querySelectorAll() But you may find it easer to add one event listener to the "parent" of these divs and continue on with the directions below. 但是,您可能会发现将一个事件侦听器添加到这些divs的“父对象”并继续下面的说明会更容易。

The event listener for mouseup will provide an event object. mouseup的事件侦听器将提供一个事件对象。 See https://developer.mozilla.org/en-US/docs/Web/Events/mouseup . 请参阅https://developer.mozilla.org/en-US/docs/Web/Events/mouseup This object has the value currentTarget which will give you the element where the mouseup event occured, which in turn is the element where the highlighting took place. 该对象的值currentTarget ,它将为您提供发生mouseup事件的元素,而该元素又是突出显示发生的元素。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM