Javascript:execCommand(" removeformat")不会删除h2标记 [英] Javascript: execCommand("removeformat") doesn't strip h2 tag

查看:77
本文介绍了Javascript:execCommand(" removeformat")不会删除h2标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调整一个所见即所得的编辑器,我正在尝试创建一个图标,它将删除 h2 的所选文本。

I'm tweaking a wysiwyg editor, and I'm trying to create an icon which will strip selected text of h2.

在以前的版本中,以下命令工作正常:

In a previous version, the following command worked perfectly:

oRTE.document.execCommand("removeformat", false, "");

但在当前版本中,虽然该命令成功从选定文本中删除了粗体,下划线等标记,斜体,它保留 h2 标签。

But in the current version, although that command successfully removes from selected text such tags as bold, underline, italics, it leaves the h2 tag intact.

(有趣的是, execCommand( formatblock...)成功创建 h2 标签。)

(Interestingly enough, execCommand("formatblock"...) successfully creates the h2 tag.)

我'我认为我将不得不放弃 execCommand 并找到另一种方式,但我也认为它不仅仅是一行代码!非常感谢您的建议。

I'm thinking that I'm going to have to abandon execCommand and find another way, but I'm also thinking that it will be a lot more than just 1 line of code! Would be grateful for suggestions.

推荐答案

您可以将格式更改为div,这不是最佳解决方案,但它有效且很短:

You can change your format to div, it's not the best solution but it works and it's short:

document.execCommand('formatBlock', false, 'div')

还有另一个解决方案是从所选文本中获取最接近的父项,然后你可以打开它,请注意这可能是某些标签,如< b> :

There is also this other solution to get the closest parent from selected text then you can unwrap it, note that this can be some tag like <b>:

var container = null;
if (document.selection) //for IE
    container = document.selection.createRange().parentElement();
else {
    var select = window.getSelection();
    if (select.rangeCount > 0)
        container = select.getRangeAt(0).startContainer.parentNode;
}
$(container).contents().unwrap(); //for jQuery1.4+

这篇关于Javascript:execCommand(&quot; removeformat&quot;)不会删除h2标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆