簡體   English   中英

ckeditor從javascript更新textarea

[英]ckeditor update textarea from javascript

我已經使用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>

當我單擊按鈕時,按鈕的值會更改,與第一個文本區域(id = noteditor)相同。
但是textarea(id = editor)不受影響。
為什么?
在調試器中,當行“ CKEDITOR.replace('editor1');” 在執行othertext函數時,我得到<exception>:TypeError。
我究竟做錯了什么?

我找到了解決問題的方法! 偶然地,我在這里找到了問題的答案: http : //sdk.ckeditor.com/samples/api.html在“使用CKEditor API”下。 我在下面重寫了我的第一個代碼:

<!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 SetContents() {
        // Get the editor instance that you want to interact with.
        var editor = CKEDITOR.instances.editor_Area1;
        var value = document.getElementById('HTMLArea' ).value;

        // Set editor content (replace current content).
        // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-setData
        editor.setData( value );
      }
    </script>
  </head>
  <body id="main">
    <input type="button" id="button" onclick="SetContents();" value="Set text" />
    <br>
    <textarea id=HTMLArea>
      <h1>
        Hello other world!
      </h1>
      <p>
        I&#39;m an other instance of <a href="http://ckeditor.com">CKEditor</a>.
      </p>
    </textarea>
    <br>
    <textarea name=text id=editor_Area1>
      <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_Area1');    
    </script>
  </body>
</html>

單擊該按鈕時,編輯區域的內容將替換為文本區域“ HTMLArea”中的html。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM