JavaScript document.execCommand remove formatting formatting?

If I format a piece of text on a page as follows:

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

What to do to remove this formatting?

+4
source share
4 answers

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

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

1 If you are not using IE

+13
source

I clear the h1 effect using this:

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

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

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

set the format in the same way as other blocks.

+1
source

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

0
source

I had the same problem when I needed to remove a tag wrapping my text.

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

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

And then check if it is nodeName "H1"; if so, then save the selected text in the selected_text variable and then delete the node itself:

elem_parent_node.remove ();

Then,

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

0
source

Source: https://habr.com/ru/post/1415065/


All Articles