题
我有此代码,用于BOLD的ExecCommand,并且它在Opera下不起作用。有人知道为什么吗?谢谢
<script type="text/javascript">
function SetToBold () {
Editor.execCommand ('bold', false, null);
}
function start() {
frames.iView.document.designMode = "On";
var myIFrame = document.getElementById("iView");
browser = navigator.appName;
if (document.all) { //IE
var iframe_window = window.frames["iView"];
iframe_window.document.body.focus();
}
else { //Firefox
field = myIFrame.contentWindow.document.getElementsByTagName("body")[0];
setTimeout("field.focus()",250);
}
if (browser == "Netscape") {
Editor = myIFrame.contentWindow.document;
Editor.designMode = 'On';
Editor.body.contentEditable = 'true';
}
else if(browser == "Microsoft Internet Explorer") {
frames['iView'].document.designMode='On';
Editor = frames['iView'].document;
}
}
我将启动功能带有身体onload。可以的是Firefox和IE,但在歌剧中没有起作用。有人知道歌剧中的execcommand的某些限制吗?谢谢
解决方案
在引用的脚本中,变量“编辑器”仅在浏览器嗅探部分中给出一个值。在Opera中,“编辑器”将不确定,因此尝试执行“ editor.execcommand()”将丢失错误。
而不是
if (browser == "Netscape") {
Editor = myIFrame.contentWindow.document;
Editor.designMode = 'On';
Editor.body.co`enter code here`ntentEditable = 'true';
}
else if(browser == "Microsoft Internet Explorer") {
frames['iView'].document.designMode='On';
Editor = frames['iView'].document;
}
您应该放下浏览器嗅探并做类似的事情
if ( myIFrame && myIFrame.contentWindow ) {
Editor = myIFrame.contentWindow.document;
Editor.designMode = 'On';
}
else if( frames['iView'] ) {
frames['iView'].document.designMode='On';
Editor = frames['iView'].document;
}
希望有帮助:)
其他提示
在给出答案之前,我想确定您的要求。该脚本正在测试Netscape或IE。开场 蜻蜓 控制台,您可以发现脚本的值。我感觉您也将在Webkit上遇到困难。
>>> var browser = navigator.appName
undefined
>>> browser
"Opera"
IVE指出,当您使用Navigator.AppName要求时,较新的Opera版本是Netscape。我记得那并不总是这样,因为我在使用常规名称的源头周围也有一些较旧的“ ifs”。不能说是什么时候,只是说。
不隶属于 StackOverflow