Rich text editor, bold text using strong tag

I am working on a very simple text editor. I read about using designMode = 'On' applied to an iframe, and then I use this to create bold text:

nameOfiframe.document.execCommand('bold',false,null); 

Even if it works, execCommand () uses b tags instead of strong to make bold text. I looked at some advanced text editor, all of them used strong instead of the b tag.

Is there an easy way for me to fix this? Or is execCommand () not suitable for use at all?

Thanks!

+6
source share
2 answers

Unfortunately, the behavior of document.execCommand() varies between browsers. As @ 1UnitedPower's answer is mentioned, document.execCommand() is for presentation, not semantic effect.

Two options are possible:

  • Write your own code to make a bold style. Unfortunately, this is not the case. You can see how large WYSIWYG editors like CKEditor and TinyMCE do it.
  • Run some code to convert the <b> elements to <strong> elements after calling document.execCommand() . It would seem that this is a simpler option. You will need some way to keep your choices when performing the conversion, if that is important to you.
+4
source

execCommand does not accept the name of the html tag as the first parameter, but a MIDAS command. See https://developer.mozilla.org/en-US/docs/Midas for a list of available commands.

This behavior is desirable because it is intended for the purposes that the wysiwyg editor should perform. WYSIWYG is not intended for semantic, but for stylistic purposes. You cannot define the semantics in bold.

However, see the insertHtml and styleWithCSS .

0
source

All Articles