本文介绍了Javascript编辑iframe时textarea的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript">
function change() {
document.getElementById('target').getElementsByTagName('html').getElementsByTagName('body').innerHTML = document.getElementById('src').value;
}
</script>

<textarea id="src" onchange="change();"></textarea><br/>
<iframe id="target"></iframe>

这是我的代码,我不明白为什么它不起作用。请不要jQuery。我知道这样的事情存在,例如我已经尝试过框架而不是iframe,而且还没有工作

This is my code and I don't understand why it isn't working. No jQuery please. I know things like this exist for example htmledit squarefree I have tried frame instead of iframe and that hasn't worked either

推荐答案

你在找什么?首先我在textarea上使用 onkeyup ,我不认为textarea有一个 onchange 事件。然后,我根据浏览器使用两种不同的方法获取iframe,然后使用textarea中的文本填充iframe。

Is this what you are looking for? First I am using onkeyup on the textarea, I don't think that textarea has an onchange event. Then I am grabbing the iframe using two different methods depending on the browser and then populating the iframe with the text from the textarea.

<script type="text/javascript">
function change() {
  var iFrame =  document.getElementById('target');
   var iFrameBody;
   if ( iFrame.contentDocument )
   { // FF
     iFrameBody = iFrame.contentDocument.getElementsByTagName('body')[0];
   }
   else if ( iFrame.contentWindow )
   { // IE
     iFrameBody = iFrame.contentWindow.document.getElementsByTagName('body')[0];
   }
    iFrameBody.innerHTML = document.getElementById('src').value;
}
</script>

<textarea id="src" onkeyup="change();"></textarea><br/>
<iframe id="target"></iframe>​

这篇关于Javascript编辑iframe时textarea的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:47