2014-07-01 52 views
0

我使用CKEDITOR的Javascript CKEDITOR獲取鼠標點擊位置

查看:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
<script src="~/Content/ckeditor/ckeditor.js"></script> 


<input id="insertPattern" type="button" value="insert pattern" /> 

@Html.TextArea("editor", new { @class = "ckeditor", id = "aboutme" }) 

的Javascript:

$(function() { 

$('input#insertPattern').click(function() { 

var txtarea = document.getElementById("aboutme"); 
var selection = txtarea.getSelection().getStartElement().getOuterHtml(); 

alert(selection); 


}}); 

如果我點擊布頓,我不能警報選擇在Ckeditor中的Html.TextArea中單擊鼠標的次數。

錯誤:

JavaScript代碼

var selection = txtarea.getSelection().getStartElement().getOuterHtml(); 

我得到以下錯誤的這一部分,

遺漏的類型錯誤未定義是不是一個函數

在哪裏我錯過?我如何獲得鼠標點擊的選擇?

回答

1

最終要插入文本的確切位置點鼠標右鍵。這段代碼就是這樣做的。

CKEDITOR.instances['aboutme'].insertText("insert some text into this string"); 
+0

謝謝這是解決方案:))) – user3722851

2

你是不是與編輯工作

CKEDITOR.instances["editorID"].getSelection().getStartElement().getOuterHtml(); 

從您的意見,您要使用

http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-insertHtml

+0

感謝您的幫助,它獲取ckeditor的文本值。我可以如何獲得鼠標選擇的位置並設置任何值? – user3722851

+0

CKEDITOR有方法插入文本/ html – epascarello

+0

var x = CKEDITOR.instances [「aboutme」]。getSelection()。getRanges [0]; alert(x);警報顯示未定義的地方,我錯過了我如何獲得選定的位置? – user3722851