简体   繁体   English

Rangy SurroundContents 替换

[英]Rangy SurroundContents Replacement

I'm looking at implementing a simple 2 button toggle on a webpage to switch some selected text between having a H1 heading and H2.我正在考虑在网页上实现一个简单的 2 按钮切换,以在具有 H1 标题和 H2 之间切换一些选定的文本。 The surroundContents method works great, however I'm encountering a problem when trying to replace an existing parent tag element node. surroundContents 方法效果很好,但是我在尝试替换现有父标记元素节点时遇到了问题。 I've played around with all sorts of ways trying to do this but not had much success.我试过各种方法来尝试做到这一点,但没有取得太大的成功。

The basic functions are below.基本功能如下。 Using the same selected text and running both of these functions one after the other will result on some output such as the following:使用相同的选定文本并依次运行这两个函数将产生如下输出:

After selecting text of "test text" and then selecting the H1 option: <h1>test text</h1>选择“test text”的文本然后选择H1选项后: <h1>test text</h1>

If the same text is selected again and this time the H2 option pressed: <h1><h2>test text</h2></h1>如果再次选择相同的文本并且这次按下了 H2 选项: <h1><h2>test text</h2></h1>

function surroundSelectedWithH1() {
    var element = document.createElement("h1");

    // removed code to setup range to save space
    if (range) {
        range.surroundContents(element);
    }
}

function surroundSelectedWithH2() {
    var element = document.createElement("h2");

    // removed code to setup range to save space
    if (range) {
        range.surroundContents(element);
    }
}

This is fine, and what would be expected, but I'm really looking for a way to remove the original parent heading element so that the heading elements do not become nested (for example - the text is surrounded by either h1 or h2, not both).这很好,这是预期的,但我真的在寻找一种方法来删除原始父标题元素,以便标题元素不会嵌套(例如 - 文本被 h1 或 h2 包围,而不是两个都)。 I did experiment accessing the parentNode etc but did not manage to get this approach functional.我确实尝试访问 parentNode 等,但没有设法使这种方法起作用。 I've tried looking at the following parentElement suggestion Getting the parent node for selected text with rangy library however I wasn't able to have rangy write the changed parent element back to the DOM or have a satisfactory way of determining where in the DOM the object was in order to replace it.我已经尝试查看以下 parentElement 建议Getting the parent node for selected text with rangy library但是我无法让 rangy 将更改的父元素写回 DOM 或者有一种令人满意的方法来确定 DOM 中的位置对象是为了替换它。 It quickly became an unwieldy approach and there must be a better option.它很快成为一种笨拙的方法,必须有更好的选择。

I do know that the rangy CssApplier module can handle this situation but I need to work with actual elements and not css.我知道 rangy CssApplier 模块可以处理这种情况,但我需要使用实际元素而不是 css。

I also noticed that on the raptor editor which uses rangy for a text editor implementation suffers from the exact same problem when applying headings: http://www.raptor-editor.com/demo我还注意到,在使用 rangy 作为文本编辑器实现的 raptor 编辑器上,在应用标题时遇到完全相同的问题: http ://www.raptor-editor.com/demo

This question was also relevant but this particular element problem can't be handled with execCommand as far as I'm aware - Javascript: how to un-surroundContents range这个问题也很相关,但据我所知,这个特定的元素问题无法用 execCommand 处理 - Javascript: how to un-surroundContents range

Any help or advice graciously received.慷慨地收到任何帮助或建议。

Try with:尝试:

highlighter.unhighlightSelection()

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

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