JavaScript document.execCommand 删除 formatBlock 格式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10810940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:06:57  来源:igfitidea点击:

JavaScript document.execCommand remove formatBlock formatting?

javascripthtmlformattingcontenteditable

提问by Greg

If I format a piece of text on a page like this:

如果我在这样的页面上格式化一段文本:

document.execCommand('formatBlock', false, 'h1');

What do I do to remove this formatting?

我该怎么做才能删除这种格式?

回答by KooiInc

I suppose document.execCommand('removeFormat',false,false)would do it?

我想document.execCommand('removeFormat',false,false)会做吗?

Issuing document.execCommand('formatBlock', false, 'div')on the <h1>-block will remove the <h1>-tag and replace it with a <div>-tag 1. Would that be viable?

document.execCommand('formatBlock', false, 'div')<h1>-block上发布将删除<h1>-tag 并将其替换为<div>-tag 1。那会可行吗?

1If you're not using IE that is

1如果您使用的不是 IE

回答by Alsophila

I clear the effect of h1 using this:

我用这个清除了 h1 的影响:

document.execCommand('formatBlock', false, 'p');

You have changed its format to h1, so we can change it back to normal paragraph format in the same way.
If your put each paragraph in a <div> , you can also use this:

您已将其格式更改为 h1,因此我们可以以相同的方式将其更改回正常的段落格式。
如果您将每个段落放在 <div> 中,您也可以使用:

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

to set the format to the same as other blocks.

将格式设置为与其他块相同。

回答by Paul Jhon Villaro

I had the same problem where I need to delete the h1 tag wrapping my text.

我遇到了同样的问题,我需要删除包装我的文本的 h1 标签。

What I did was get the parent node of the selected text:

我所做的是获取所选文本的父节点:

var elem_parent_node = window.getSelection().getRangeAt(0).startContainer.parentNode;

var elem_parent_node = window.getSelection().getRangeAt(0).startContainer.parentNode;

And then check if it's nodeName is "H1"; if yes, then store the selected text in a selected_text variable and then delete the node itself:

然后检查它的nodeName是否为“H1”;如果是,则将所选文本存储在 selected_text 变量中,然后删除节点本身:

elem_parent_node.remove();

elem_parent_node.remove();

Then,

然后,

document.execCommand('insertText', false, select_text);

document.execCommand('insertText', false, select_text);

回答by Paul Jhon Villaro

To replace the h1 with its text content node, then use the code below. For a complete code, you would need to add some checks to make sure you remove what you want.

要将 h1 替换为其文本内容节点,请使用以下代码。对于完整的代码,您需要添加一些检查以确保删除您想要的内容。

const button = document.getElementsByTagName('button')[0]
button.addEventListener('click', event => {
  const selection = window.getSelection()
  if (!selection.isCollapsed) {
    selection.anchorNode.parentNode.replaceWith(selection.anchorNode)
  }
})
<div contenteditable="true">
  <p>Some text</p>
  <h1>Select me then click the button</h1>
  <p>Some text</p>
</div>
<button>Click to remove H1</button>

回答by user1123382

you may have to find the parent tag, then use innerHTML to get the text and replace the original data between the parent tag and end-tag with the innerHTML. This would however remove all formatting.

您可能需要找到父标签,然后使用innerHTML 获取文本并将父标签和结束标签之间的原始数据替换为innerHTML。但是,这将删除所有格式。