JavaScript:如何从节点中删除标签?

发布于 2024-10-23 15:41:23 字数 2110 浏览 6 评论 0原文

在上一个问题中,有人将我置于“rangy”http://code.google.com/p/范围广泛/。即使我不完全理解它,这也很有趣。我不是 JavaScript 人。然而,我已经成功地用它做了我需要的大部分事情,除了 1。这个概念是一个非常基本的 RTE,只是粗体、斜体等。我设法做到了,创建了一个链接 - 也做到了,好吧,可能有什么一个 JS 人花了 2 分钟就花了我好几个小时——令人沮丧,但我想我正在学习一点——非常慢。无论如何,使用 rangy 我可以创建(请原谅糟糕的代码)这样​​的 href 链接:

$('#linkbut').live('click',function(){
        var sel = rangy.getSelection();     
        var range = sel.getRangeAt(0); 
        range.splitBoundaries(); 
        var textNodes = range.getNodes([3]); 
            for (var i = 0, len = textNodes.length; i < len; ++i) { 
            var newLink = document.createElement('a');
            newLink.setAttribute('href','test.html');
            var linkText = document.createTextNode(sel);
            var parent = textNodes[i].parentNode;
            parent.insertBefore(newLink,textNodes[i]); 
            newLink.appendChild(linkText);
            range.deleteContents();
            }
    });

#linkbut 是一个简单的 HTML 按钮,上面的实际 href (test.html) 将来自输入的值字段而不是“硬编码”。但如果我想删除它,我无法完成的是“删除”链接。

进一步的解释:一旦创建了链接,我可能想删除它,所以我尝试了上面代码的“反向” - 显然没有好处,所以已经得到“到目前为止”:

$('#deletelink').live('click',function(){
                var sel = rangy.getSelection();     
                var range = sel.getRangeAt(0); 
                range.splitBoundaries(); 
                var textNodes = range.getNodes([3]); 
                var txt = sel.toString();
                range.deleteContents();
                var replaceText = document.createTextNode(txt);
                sel.appendChild(replaceText);

            });

我真正想做的事情(可能不是可能)是有一些“通用”功能,可以从上面的节点中删除任何标记元素,我想做的是:

  1. 获取范围 - sel = rangy.getSelection();
  2. Turn " sel" 转换为字符串变量 var txt = sel.toString();
  3. 删除内容 - 包括 a 元素 range.deleteContents(); 然后
  4. 将删除的内容替换为“文本”版本 var ReplaceText = document.createTextNode(txt); sel.appendChild(replaceText);

我得到“到目前为止”内容已被删除,但我无法让“新文本替换”发挥作用。

希望一切都清楚了 - 因为这对我来说不是;)

In a previous question someone put me on to "rangy" http://code.google.com/p/rangy/. It's interesting even if I don't fully understand it. I am not a JavaScript person. However, I have managed to do most things with it that I need with the exception of 1. The concept is a very basic RTE, just bold, italic etc. I managed that, created a link - done that too, OK what might have taken a JS guy 2 mins has taken me hours and hours - frustrating but I think I am learning a bit - very slowly. Anyhow, using rangy I can create (excuse poor code) an href link like this:

$('#linkbut').live('click',function(){
        var sel = rangy.getSelection();     
        var range = sel.getRangeAt(0); 
        range.splitBoundaries(); 
        var textNodes = range.getNodes([3]); 
            for (var i = 0, len = textNodes.length; i < len; ++i) { 
            var newLink = document.createElement('a');
            newLink.setAttribute('href','test.html');
            var linkText = document.createTextNode(sel);
            var parent = textNodes[i].parentNode;
            parent.insertBefore(newLink,textNodes[i]); 
            newLink.appendChild(linkText);
            range.deleteContents();
            }
    });

#linkbut is a simple HTML button and the actual href (test.html) above will come from the value of an input field and not be "hard coded". But what I cannot get done is "delete" the link if I want to remove it.

Further explanation: Once the link is created I may want to delete it so I have tried the "reverse" of the code above - obviously no good, so have got "this far":

$('#deletelink').live('click',function(){
                var sel = rangy.getSelection();     
                var range = sel.getRangeAt(0); 
                range.splitBoundaries(); 
                var textNodes = range.getNodes([3]); 
                var txt = sel.toString();
                range.deleteContents();
                var replaceText = document.createTextNode(txt);
                sel.appendChild(replaceText);

            });

What I really would like to do (may not be possible) is to have some "generic" function that removes ANY tag element from a node in the above what I am trying to do is:

  1. Get the range - sel = rangy.getSelection();
  2. Turn "sel" into a string variable var txt = sel.toString();
  3. Delete the content - including the a elements range.deleteContents();
    then
  4. Replace the deleted with the "text" version var replaceText = document.createTextNode(txt); sel.appendChild(replaceText);

I get "so far" the content is deleted BUT I cannot get the "new - text replacement" to function.

Hope all is clear - cos it isn't to me ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风透绣罗衣 2024-10-30 15:41:23

我知道一个老问题,但我遇到了同样的问题,并在此处找到了可行的解决方案。

var el = document.getElementsByTagName('span')[0]; // Get the element in question
var pa = el.parentNode;

while(el.firstChild) {
    pa.insertBefore(el.firstChild, el);
}

pa.removeChild(el);
pa.normalize();

编辑: Normalize() 方法很重要,这样您就不会拥有一堆相邻的文本节点。它使所有文本节点再次聚合成一个文本节点。

对于您的确切问题,您可以迭代所有节点,然后执行上面的代码。

I know an old question but I had the same issue and found a viable solution here.

var el = document.getElementsByTagName('span')[0]; // Get the element in question
var pa = el.parentNode;

while(el.firstChild) {
    pa.insertBefore(el.firstChild, el);
}

pa.removeChild(el);
pa.normalize();

Edit: The normalize() method is important so that you don't have a bunch of adjacent text nodes. It causes all the text nodes to be aggregated into one text node again.

For your exact question, you could iterate over all the nodes and then execute the above code.

寄居者 2024-10-30 15:41:23

您可能应该查找如何在 jQuery 中进行嵌套选择,并且正如 NeXXeuS 所说,您应该考虑通过所选方法上的 .html() 删除内容。

如果您有:

<div id="mydiv"></div>

并且您运行: `

$('#mydiv').append('<a href="test.html">Test Link</a>');

html 将如下所示:

<div id="mydiv"><a href="test.html">Test Link</a></div>

您可以通过以下方式选择链接:

$('#mydiv a');

您可以通过执行以下操作来删除它:

$('#mydiv').html('');

You should probably look up on how to do nested selection in jQuery and as NeXXeuS said you should look into removing the contents via .html() on your selected method.

If you have:

<div id="mydiv"></div>

And you run: `

$('#mydiv').append('<a href="test.html">Test Link</a>');

The html will look like:

<div id="mydiv"><a href="test.html">Test Link</a></div>

You can this select the link with:

$('#mydiv a');

You can delete it by doing:

$('#mydiv').html('');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文