如何以编程方式确定 CKEditor 实例的名称

发布于 2024-08-27 10:29:25 字数 482 浏览 4 评论 0原文

我已以编程方式将 CKEditor 实例添加到 ASP.NET 页面的代码隐藏中的页面:

VB.NET:

itemEditor = New CkEditor
cell.Controls.Add(itemEditor)

... 工作正常。我可以获取回发的 HTML 并用它做一些事情。

但是,我还想用它做一些客户端操作,特别是从另一个控件中取出选定的项目,并通过处理 onchange 事件将其插入到文本中。

那么,如何在 JavaScript 中获取编辑器实例的名称,以便我可以执行以下操作:

function GetCkText()
{
    var htmlFromEditor = CKEDITOR.instances['editorName'].getData();
    // do stuff with htmlFromEditor
}

I've added a CKEditor instance programmatically to my page in the code-behind of my ASP.NET page:

VB.NET:

itemEditor = New CkEditor
cell.Controls.Add(itemEditor)

... which works fine. I can get the HTML on the postback and do stuff with it.

However, I also want to do some client-side stuff with it, specifically take a selected item out of another control, and insert it into the text by handling the onchange event.

So, how can I get the name of the editor instance in the JavaScript, so that I can do stuff like:

function GetCkText()
{
    var htmlFromEditor = CKEDITOR.instances['editorName'].getData();
    // do stuff with htmlFromEditor
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦里梦着梦中梦 2024-09-03 10:29:25

假设您只有一个编辑器实例:

for ( var i in CKEDITOR.instances ){
   var currentInstance = i;
   break;
}
var oEditor   = CKEDITOR.instances[currentInstance];

以下是 JavaScript API 关于 实例< 的说法/a>.

这是定义 CKEditor 的另一种方法。这里“fck”是输入字段 id:

CKEDITOR.replace( 'fck', {
    customConfig : prefix + 'js/ckeditor/config.js',
    height: 600,
    width: 950
});

editor = CKEDITOR.instances.fck;

请注意我如何使用 .fck 引用该实例。

Assuming you only have one editor instance:

for ( var i in CKEDITOR.instances ){
   var currentInstance = i;
   break;
}
var oEditor   = CKEDITOR.instances[currentInstance];

Here is what the JavaScript API says about instances.

Here is another way of defining the CKEditor. Here 'fck' is the input fields id:

CKEDITOR.replace( 'fck', {
    customConfig : prefix + 'js/ckeditor/config.js',
    height: 600,
    width: 950
});

editor = CKEDITOR.instances.fck;

Notice how I am then able to reference the instance using .fck.

陈甜 2024-09-03 10:29:25

如果您只有一个实例并且您不知道它的名称。

CKEDITOR.instances[Object.keys(CKEDITOR.instances)[0]].getData()

If you only have a single instance and you do not know the name of it.

CKEDITOR.instances[Object.keys(CKEDITOR.instances)[0]].getData()
静谧幽蓝 2024-09-03 10:29:25

以下代码:

var allInstances=CKEDITOR.instances;
for ( var i in allInstances ){
    alert(allInstances[i].name);
}

对我来说效果很好。

The following code:

var allInstances=CKEDITOR.instances;
for ( var i in allInstances ){
    alert(allInstances[i].name);
}

works fine for me.

拧巴小姐 2024-09-03 10:29:25

好吧,我找到了一种方法...但我不太喜欢它...

我在添加编辑器后向页面添加了一个隐藏字段控件,并将编辑器的 ClientId 放入其值中:

Dim hdn As New HiddenField
With hdn
    .ID = "HiddenField"
    .Value = itemEditor.ClientID
End With
cell.Controls.Add(hdn)

.. 和然后在 JavaScript 中,我可以获得隐藏字段,因此编辑器名称如下:

function GetCkText()
{
    var hdn = document.getElementById("HiddenField");
    var editorName = hdn.getAttribute("value");
    var editor = CKEDITOR.instances[editorName];
    alert(editor.getData());
    return false;
}

但至少可以说,它有点不优雅。有人有更好的办法吗?

Well I've found a way... but I don't like it much...

I've added a Hidden Field control to the page, after adding the editor, and put the editor's ClientId in its value:

Dim hdn As New HiddenField
With hdn
    .ID = "HiddenField"
    .Value = itemEditor.ClientID
End With
cell.Controls.Add(hdn)

.. and then in the JavaScript, I can get the hidden field, and hence the editor name as follows:

function GetCkText()
{
    var hdn = document.getElementById("HiddenField");
    var editorName = hdn.getAttribute("value");
    var editor = CKEDITOR.instances[editorName];
    alert(editor.getData());
    return false;
}

But it's a bit inelegant, to say the least. Anyone got a better way?

过期情话 2024-09-03 10:29:25

如果您使用 CKEDITOR.appendTo(...),请记住 ckeditor 会在内部创建实例名称。因此,您可以在创建名称后立即查询该名称,然后将其存储在某处,并在以后使用。

var lvo_editor = CKEDITOR.appendTo( "my_div" , null , lvs_html ) ; 
my_global_var  = lvo_editor.name                                 ;

顺便说一句:CKEDITOR.replace(...) 方法允许您定义实例名称(请参阅上面的答案)

If you are using CKEDITOR.appendTo(...), keep in mind that the ckeditor does create an instance name internally. So you can query for that name immediately after creating it, then store it somewhere, and use it later.

var lvo_editor = CKEDITOR.appendTo( "my_div" , null , lvs_html ) ; 
my_global_var  = lvo_editor.name                                 ;

by the way: The CKEDITOR.replace(...) method allows you to define an instance name (see answer above)

你在看孤独的风景 2024-09-03 10:29:25

如果您需要来自插件的实例,至少在版本 4+ 中您可以执行此操作。

CKEDITOR.currentInstance

在这里我想知道我应用 ckeditor 的文本区域的名称。

CKEDITOR.currentInstance.name

If you need the instance from a plugin, at least in version 4+ you can do this.

CKEDITOR.currentInstance

Here I am wanting to know the name of the textarea I applied ckeditor on.

CKEDITOR.currentInstance.name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文