简体   繁体   English

在contenteditable div中的插入位置之前和之后获取节点/元素

[英]Get node/element before and after caret position in contenteditable div

I have a contenteditable div in my GWT application and when I press backspace or delete key, I want to get the node before and after caret position and check whether it is a text node or not. 我的GWT应用程序中有一个contenteditable div,当我按退格键或删除键时,我想在插入位置之前和之后获取节点并检查它是否是文本节点。

Element element = DOM.createDiv(); 
element.setAttribute(contenteditable, "true");
basePanel.getElement().appendChild(element);

This is how I created the content editable div. 这就是我创建内容可编辑div的方法。

Any solutions will be appreciative. 任何解决方案都将受到赞赏。

Regards. 问候。

Dig into selection and range classes. 深入选择和范围类。 They are not yet browser compatible, so you might use 它们还不兼容浏览器,因此您可以使用它们

https://code.google.com/p/rangy/ or https://code.google.com/p/rangy/
jquery++ jQuery的++

for abstraction. 用于抽象。 You then create a range for your div, expand it by 1 on both ends. 然后,为div创建一个范围,在两端将其展开1。 then you examine startcontainer and endcontainer to find out their node type. 然后检查startcontainer和endcontainer以找出它们的节点类型。

The selection and range apis are not overly beautiful and working with them is more involved than necessary, but that is the way to get it done. 选择和范围apis不是太漂亮,与它们合作比必要更多,但这是完成它的方法。

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

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