JavaScript:如何从节点中删除标签?
在上一个问题中,有人将我置于“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);
});
我真正想做的事情(可能不是可能)是有一些“通用”功能,可以从上面的节点中删除任何标记元素,我想做的是:
- 获取范围 -
sel = rangy.getSelection();
- Turn " sel" 转换为字符串变量
var txt = sel.toString();
- 删除内容 - 包括
a
元素range.deleteContents();
然后 - 将删除的内容替换为“文本”版本 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:
- Get the range -
sel = rangy.getSelection();
- Turn "sel" into a string variable
var txt = sel.toString();
- Delete the content - including the
a
elementsrange.deleteContents();
then - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道一个老问题,但我遇到了同样的问题,并在此处找到了可行的解决方案。
编辑: Normalize() 方法很重要,这样您就不会拥有一堆相邻的文本节点。它使所有文本节点再次聚合成一个文本节点。
对于您的确切问题,您可以迭代所有节点,然后执行上面的代码。
I know an old question but I had the same issue and found a viable solution here.
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.
您可能应该查找如何在 jQuery 中进行嵌套选择,并且正如 NeXXeuS 所说,您应该考虑通过所选方法上的 .html() 删除内容。
如果您有:
并且您运行: `
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:
And you run: `
The html will look like:
You can this select the link with:
You can delete it by doing: