2014-01-15 55 views
0

我正在深入探索這個神奇的HTML世界。我有CKEditor工作,myFunction會產生警報。我需要在編輯器中獲取文本的HTML。如何獲取CKEditor的html

我得到這個從另一篇文章:

CKEDITOR.instances.textarea.on('instanceReady', function(instanceReadyEventObj) 
{ 
var editorInstanceData = CKEDITOR.instances.textarea.getData(); 
alert(editorInstanceData); 
}); 

我無法發表評論或回覆的帖子,因爲我沒有足夠的積分,所以我不得不問一個副本。 我有警告工作,並粘貼myFunction中的代碼,但我得到控制檯中的錯誤未捕獲TypeError:無法調用未定義的方法。

<!DOCTYPE html> 
<html> 


<head> 
<script> 
function myFunction() 
{ 

var htmldata = CKEDITOR.instances.Editor.document.getBody().getHtml(); 
alert(htmldata); 
} 
</script> 
    <title>A Simple Page with CKEditor</title> 
    <!-- Make sure the path to CKEditor is correct. --> 
    <script src="http://www.wilsea.com/ckeditor/ckeditor.js"></script> 
</head> 
<body> 
    <form> 
     <button onclick="myFunction()">Click me</button> 

     <textarea id="editor1" name="editor1" rows="10" cols="80"> 
      This is my textarea to be replaced with CKEditor..... 
     </textarea> 
     <script> 
      // Replace the <textarea id="editor1"> with a CKEditor 
      // instance, using default configuration. 
      CKEDITOR.replace('editor1'); 
     </script> 
    </form> 
</body> 

MrWarby

<!DOCTYPE html> 
<html> 
<head> 

<script type="text/javaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
<script src="http://www.wilsea.com/ckeditor/ckeditor.js"></script> 

</head> 
<body> 
     <textarea id="editor1" name="editor1" rows="10" cols="80"> 
      This is my textarea to be replaced with CKEditor 
     </textarea> 

<script> 
function myFunction() 
{ 

alert('test'); 

CKEDITOR.instances.textarea.on('instanceReady', function(instanceReadyEventObj) 
{ 
var editorInstanceData = CKEDITOR.instances.textarea.getData(); 
alert(editorInstanceData); 
}); 


} 
CKEDITOR.replace('editor1'); 
</script> 

<p>Click the button to trigger a function.</p> 

<button onclick="myFunction()">Click me</button> 

<p id="demo"></p> 

</body> 
</html> 

我已經試過移動到不同的地方,但我仍然得到同樣的錯誤。

測試頁面是在http://www.wilsea.com/ckeditor/testckeditor.html

MrWarby。

+0

任何人都可以糾正我的代碼,使其正確顯示? – user3193843

回答

0

之前您在HEAD腳本中加入兩行:

<script type="text/javaScript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> 
<script src="/ckeditor/ckeditor.js"></script> 

但變化路徑CKEDITOR腳本。

+0

嗨,我已經更新了這個問題。 – user3193843