Call CKEditor by class

萝らか妹 提交于 2019-12-05 03:47:28

<!DOCTYPE HTML>
<html>
	<head>
		<script src="ckeditor.js"></script>
		<script>
			$(document).ready(function() {
				CKEDITOR.replaceClass = 'ckeditor';
			});
		</script>
	</head>
	<body>
		<div>
			<textarea class="ckeditor" cols="80" name="content"></textarea>
		</div>
		<div>
			<textarea class="ckeditor" cols="80"  name="content"></textarea>
		</div>			 
	</body>
</html>

There is already a class replacer built into CKEditor. Any textarea with the class ckeditor applied will have the editor applied automatically without initialisation.

The code is in ckeditor.js and uses CKEDITOR.replaceClass="ckeditor";


Initialise by class (no extra JavaScript required)

Add Class ckeditor to textarea

HTML

<textarea class="ckeditor" rows="4" cols="50"></textarea> 

.NET

<asp:TextBox ID="txt" CssClass="ckeditor" runat="server" TextMode="MultiLine" />

Custom Class initialisation

If you wish to use your own class you can simply overwrite the class with your own initialisation after ckeditor.js

Use:

CKEDITOR.replaceClass="MyClass";

Personally I use function like this :

function loadEditor(id){
    var instance = CKEDITOR.instances[id];
    if(instance){
        CKEDITOR.destroy(instance);
    }
    var editor = CKEDITOR.replace(id);
}

I use it with lots of dynamic config, and I am sure it can be nicely changed to suit your needs. Have a play and let us know what you come with!

As you can see I am using ids anyway, but can't see a problem with using classes

<!DOCTYPE HTML>
<html>
	<head>
		<script src="ckeditor.js"></script>
		<script>
			$(document).ready(function() {
				CKEDITOR.replaceClass = 'ckeditor';
			});
		</script>
	</head>
	<body>
		<div>
			<textarea class="ckeditor" cols="80" name="content"></textarea>
		</div>
		<div>
			<textarea class="ckeditor" cols="80"  name="content"></textarea>
		</div>			 
	</body>
</html>

Here's a minimalist example that illustrates using the ckeditor class on text areas, based on a stripped down version of http://nightly.ckeditor.com/latest/standard/ckeditor/samples/old/replacebyclass.html:

<script src="https://cdn.ckeditor.com/4.5.11/standard/ckeditor.js"></script>

<textarea id="something" class="ckeditor">
    &lt;p&gt; Hello world! &lt;/p&gt;
</textarea>

In my testing I found that the <textarea> tag needed to have the ckeditor class, and it needed to have an id or name field present.

...which is a bit strange, because the Replace by Class sample contains the following code:

<textarea class="ckeditor" cols="80" id="editor1" name="editor1" rows="10">

And it works perfectly fine.

Anastasios Barous

The best way to instantiate multiple ckeditor4 instances is using the ckeditor-jquery adapter.

Then only

$( 'textarea.ckeditor' ).ckeditor(); 

is enough to have multiple ckeditor instances.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!