简体   繁体   English

IE11中不会取消document.execCommand的粗体显示

[英]document.execCommand won't unbold in IE11

I am trying to use an iframe (in designMode) as a rich text editor. 我试图将iframe(在designMode中)用作富文本编辑器。

<iframe name="richTextField" id="richTextField" class="rte"></iframe>

I also have a button, which can bold/unbold text in the iframe by calling a javascript function iBold. 我还有一个按钮,可以通过调用javascript函数iBold来加粗/取消加粗iframe中的文本。

<a href="javascript:iBold()" type="button" value="bold"><img src="/images/bold.gif" id="boldButton"/></a>

function iBold()
{
    richTextField.document.execCommand('bold');
    console.log(window.frames['richTextField'].document.body.innerHTML);
}

The bold button works for most cases, but there is one case that it does not work (in IE11). 粗体按钮适用于大多数情况,但是有一种情况它不起作用(在IE11中)。 If I select (highlight) small portions of the text in the iframe, I can bold it and also unbold it with the bold button, but if I select ALL text in the iframe, I can bold it, but I can never unbold it. 如果在iframe中选择(突出显示)文本的一小部分,则可以将其加粗,也可以使用粗体按钮将其展开。但是,如果在iframe中选择“所有”文本,则可以将其加粗,但是我永远都无法对其进行展开。 The text is forever bold after that point; 在那之后,文本永远是粗体的。 no matter what I select/highlight, execCommand won't unbold anything. 无论我选择/突出显示什么,execCommand都不会展开任何内容。 Note: it works fine in Firefox of course. 注意:它当然可以在Firefox中正常工作。

http://jsbin.com/hiwetumobo/1/edit?html,js,output http://jsbin.com/hiwetumobo/1/edit?html,js,output

It's not really the best solution but the following will work, at least. 这并不是真正的最佳解决方案,但至少可以使用以下方法。

if(isIE11) {
 isBolded = yourDocumentHere.queryCommandValue('bold');
 if(isBolded) {
   yourDocumentHere.execCommand('removeFormat', false)
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM