2014-01-15 32 views
1

看着http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#getData後,我已經到位editor1在嘗試了一些變化:如何以編程方式查詢CKEDITOR內容?

var editor_contents = CKEDITOR.instances.editor1.getData(); 

Chrome的控制檯報告說,它無法讀取的未定義的屬性的getData。我試過了引用的那一行,以及editor0和scratchpad(後者是TEXTAREA的HTML ID,我給CKEDITOR)。他們都得到相同的錯誤。

我該如何(或應該)調用getData()來獲取TEXTAREA的HTML ID'scratchpad'的內容?我試着查詢jQuery('#scratchpad'),但是我得到的返回值是用來初始化#scratchpad的值,並且沒有顯示後續編輯。

我很想知道如何以編程方式查詢CKEDITOR小部件的實時內容。

謝謝,

- 編輯 -

我試圖檢查元件,並且基於該嘗試了以下:

變種editor_contents = jQuery的('TD#cke_contents_scratchpad IFRAME body.cke_show_borders 「)的.html();

有一個未定義的值。我的猜測是,Chrome的檢查員以方便的方式呈現iframe,但不是以jQuery的方式呈現。 (那是猜測關閉基地?)

回答

2

聽起來像是你不是錯誤的ID叫,或者如果你想使用jQuery的,你有沒有更新textarea元素。

要解決「我從得到的返回值是用來填充最初的#scratchpad價值,並沒有表現出後續的編輯」需要調用updateElement()。看到這個答案獲取值之前如何做到這一點的更多細節:https://stackoverflow.com/a/9924844/694325

但我也會檢查,如果你確實設置了正確的id和你的元素是editor1。如果我們看到您的HTML和您用來替換編輯器的JavaScript,這將有所幫助。

1

嘗試

<script> 
$(document).ready(function(e) { 
    var config = { 
     extraPlugins: 'htmlwriter', 
     height: 100, 
     width: '100%', 
     toolbar: [ 
      ['Source', '-', 'Bold', 'Italic', 'Underline', '-', ], 
      ['TextColor', '|', 'Maximize', 'Preview'], 
     ] 
    }; 
    CKEDITOR.replace("mytextareaname", config); 

    $("#mybutton").click(function(){ 
     var editor_contents = CKEDITOR.instances["mytextareaname"].getData(); 
     alert(editor_contents); 

    }) 
}); 
</script> 
</head> 
<body> 
<textarea id="mytextareaname" name="mytextareaname"></textarea> 
<input type="button" value="Testar Editor" id="mybutton" /> 
+0

Hrm?這兩個在JavaScript中是等價的,並且它們適當地給出相同的錯誤。 – JonathanHayward

+0

我知道它是相同的。 我也無法使用它。 我會發送代碼。 –

+0

它可能對你有用。 –

1

如果您正在執行上的document.ready或在window.onload中的getData()函數,你必須在你的編輯器沒有當前的內容,那麼當然它會返回undefined。

將您的getData()調用放入一個函數中,一旦您將某些內容輸入到文本編輯器中,就可以調用該函數。下面是它可能是什麼樣子:

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Editor</title> 
    <script src="ckeditor/ckeditor.js"></script> 
</head> 
<body> 
    <textarea name="editor1" id="editor1" cols="30" rows="10"></textarea> 
    <!-- Button that triggers below function--> 
    <button onclick="logEditorData()">Log the Content!</button> 
    <script> 
     // instantiate the editor 
     CKEDITOR.replace('editor1'); 

     // declare function that you'll call with above button 
     // once you've type some words into the editor 
     function logEditorData() { 
      var editorData = CKEDITOR.instances.editor1.getData(); 
      console.log(editorData); 
     }; 

    </script> 
</body> 
</html> 
相關問題