简体   繁体   English

查找范围开始或结束的元素

[英]Find the Element in which a Range Starts or Ends

I am trying to find the element in which a Selected Range Starts. 我试图找到选定范围开始的元素。 For example: 例如:

<p>Hello, <span class="editet">Genei180</span> is My Name</p>

The user now Selects: "ei180 is My Name" 用户现在选择:“ ei180是我的名字”

Therefore i want something like startElementofRange() which Returns "object HTMLSpanElement" 因此,我想要类似startElementofRange()的东西,该东西返回“ object HTMLSpanElement”
I than want that on keypress only the Part which sits in Span Edíted gets Deleted. 我不希望在按键时仅删除位于SpanEdíted中的部分。

I know about the Node.startOffset and Node.startContainer. 我知道Node.startOffset和Node.startContainer。 But Node.startContainer would return in this case the whole p-Element as Text Element. 但是在这种情况下,Node.startContainer将返回整个p元素作为Text元素。 But Node.startOffset is in this Case Relative to the Span Element so it would be 2. Also there can be occasions where more than one Span Edited Element can be selected. 但是,在这种情况下,Node.startOffset相对于Span元素为2。在某些情况下,可以选择多个Span Edited Element。 Current Approach is to loop true them all: 当前的方法是使它们全部循环:

Sitenote: Sitenote:

This is for a Website which allows editing true the Javascript tag Contenteditable='true' and Displays Everything that changed in Green. 这是针对允许编辑true的Javascript标签Contenteditable ='true'并显示以绿色显示的所有内容的网站。 I also want to read in only the Changed Parts from the Spans if Submited. 如果提交,我也只想阅读跨度中的更改零件。 So it can be Disscussed by the Users and Voteaccepted into an Articel. 因此,可以由用户进行讨论并接受投票。 Is this in general a Secure and sense Making approach? 这通常是一种安全且有意义的方法吗?

I searched for weeks... 我搜索了几周...

range.startContainer returns the Notetype but not as i thought of the p Element instead it returns the type of the Span Element. range.startContainer返回Notetype,但不像我想到的p元素,而是返回Span元素的类型。

So you just need to use range.startContainer.parentNode to get The Element from it. 因此,您只需要使用range.startContainer.parentNode即可从中获取元素。

I feel stupid now. 我现在觉得很蠢。 I leaf this up in case some one has the Same Problem 如果有人遇到相同的问题,我会留意

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

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