document.execCommand() FontSize in pixels?

后端 未结 8 1977
一向
一向 2020-11-29 06:55

How can I change font size to 30px (for example) using document.execCommand?

This:

document.execCommand(\"fontSize\", false, \"30px\");
         


        
8条回答
  •  星月不相逢
    2020-11-29 07:28

    It's a limitation of the FontSize command. There are various options I can think of:

    • You could use a CSS class instead and use the CSS class applier module of my Rangy library;
    • You could use a hacky method, such as calling document.execCommand("fontSize", false, "7"); and then finding the elements the command has created and changing them as required. See example: http://jsfiddle.net/S3ctN/. This obviously depends on there being no other elements with size 7 in the document and it also relies on the browser using elements for font size, which it seems they all do.

提交回复
热议问题