ckeditor从javascript更新textarea

时间:2017-03-23 14:19:37

标签: javascript html ckeditor textarea

我已经使用了一年的CKEditor,一切都按原样运作 现在我需要使用javascript更改textarea中的文本 我创建了一个例子来说明我的问题。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>CKEditor Sample</title>
    <script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
    <script type="text/javascript">
      function othertext() {
        document.getElementById('button').value = 'xxx';
        document.getElementById('noteditor').innerHTML = '<h1>Hello other world!</h1><p>I&#39;m also an instance of <a href="http://ckeditor.com">CKEditor</a>.</p>';
        document.getElementById('editor').innerHTML = '<h1>Hello other world!</h1><p>I&#39;m also an instance of <a href="http://ckeditor.com">CKEditor</a>.</p>';
        CKEDITOR.replace('editor');    
      }  
    </script>
  </head>
  <body id="main">
    <input type="button" id="button" onclick="othertext();" value="NewText" />
    <br>
    <textarea id=noteditor>
      <h1>
        Hello world!
      </h1>
      <p>
        I&#39;m an instance of <a href="http://ckeditor.com">CKEditor</a>.
      </p>
    </textarea>
    <br>
    <textarea name=text id=editor>
      <h1>
        Hello world!
      </h1>
      <p>
        I&#39;m an instance of <a href="http://ckeditor.com">CKEditor</a>.
      </p>
    </textarea>
    <script type="text/javascript">   
      CKEDITOR.replace('editor');    
    </script>
  </body>
</html>

单击按钮时,按钮的值会发生变化,与第一个textarea(id =注册商)相同。
但textarea(id =编辑)不受影响 为什么呢?
在调试器中,当行“CKEDITOR.replace('editor1');”在函数othertext中执行我得到&lt; exception&gt;:TypeError。
我做错了什么?

1 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法! 我偶然在这里找到了我的问题的答案:http://sdk.ckeditor.com/samples/api.html&#34;使用CKEditor API&#34;。 我在下面重写了我的第一个代码:

for (var p in promotions) {
  if (promotions.hasOwnProperty(p)) {
    console.log(p);
  }
}

单击按钮时,编辑区域的内容将替换为textarea&#34; HTMLArea&#34;中的html。