Why do they prefer using iframe and turn on design mode by .contentWindow.document.designMode = "on"
instead of d开发者_JAVA技巧irectly using textarea?
The reason is that you won't be able to live preview the styles applied using your editor in a textarea. Whenever you apply styles like pressing Ctrl + B
the section inside the iframe can render the characters in bold format whereas in a textarea it won't be possible. Textarea is for entering non formatted values whereas a design mode turned on iframe is for entering formatted text.
You can use any other element like div and can turn that into an editor. But using iframe you can have a separate document and can manipulate that without considering the parent page in which the iframe is set.
I suppose using an iframe allows for better isolation : the editor can do pretty much whatever it wants, without any risk of conflict with the rest of your page -- especially for CSS and Javascript.
Most browsers have had some kind of editable mode for quite a while but until quite recently some browsers (such as Firefox until version 3) only supported designMode
, which works only on a whole document, rather than the more flexible contentEditable
, which can be switched on and off for individual elements within a document. WYSIWYG editors were therefore obliged to use an iframe to support such browsers.
精彩评论