嵌入在iframe中的Textarea在designMode = On时变为不可编辑

时间:2012-07-27 22:22:52

标签: javascript iframe textarea text-editor

如您所见,下面我有两个用于使文本变为粗体的函数: doRichEditCommand() SetToBold()

我的目标首先是让 doRichEditCommand()iframe中嵌入的 textarea 中工作,因为它可以在所有主流浏览器上完美运行,然后让像 SetToBold()这样的 execCommand 函数起作用,因为我不确定它们在 iframe 中是否是跨浏览器。

要使第一个功能起作用,我已启用 designMode 。但是,启用它后, textarea 将无法使用。

如何修改以下代码,以便我的功能在textarea嵌入的iframe内工作?

提前感谢您的帮助。

<html>
<head>
<script type="text/javascript">

function getIFrameDocument(aID){
// if contentDocument exists, W3C compliant (Mozilla)
if (document.getElementById(aID).contentDocument){
return document.getElementById(aID).contentDocument;
} else {
// IE
return document.frames[aID].document;
 }
}

function load(){
getIFrameDocument("editorWindow").designMode = "On";
}

function doRichEditCommand(aName, aArg){
getIFrameDocument('editorWindow').execCommand(aName,false, aArg);
document.getElementById('editorWindow').contentWindow.focus()
} 

function SetToBold () {
        document.execCommand ('bold', false, null);
    }
</script>
</head>
<body onload="load()">
  <iframe id="editorWindow" src="you.php"></iframe>
  <button onclick="doRichEditCommand('bold')" style="font-weight:bold;">B</button>
  <button onclick="SetToBold();">Bold</button>
</body>
</html>

P.S。我没有包含“you.php”,因为它只是必要的标签和textarea。

0 个答案:

没有答案