如何让CKEditor用CSS渲染文本?

发布于 2024-08-14 12:36:52 字数 660 浏览 6 评论 0原文

我有一个 CKEditor 用于编辑网页中的文本。

在网页中,文本在其上下文中呈现,因此遵循页面 CSS 格式。

我的问题是如何告诉 CKEditor 将 CSS 样式表应用于编辑器渲染?当然不改变生成的源?

我的代码:

<textarea class="ActuContent" name="actu-content" cols="100" rows="20">my content></textarea>
<script type="text/javascript">
        window.onload = function()
        {
                CKEDITOR.replace( 'actu-content' );
        };
</script>

和我的 CSS:

.ActuContent{
    padding:10px 10px 10px 10px;
    color:#416a8b;
    font-size:1.6em;
}

我的 CKEditor Config.js 文件仅包含工具栏配置。

CKeditor 不会将“.ActuContent”的设置应用于其渲染...

I have a CKEditor used to edit a text in a web-page.

In the web-page, the text renders in its context and therefore follows the page CSS formatting.

My question is how to tell CKEditor to apply a CSS style-sheet to the editor rendering ? Without of course changing the generated source ?

My code :

<textarea class="ActuContent" name="actu-content" cols="100" rows="20">my content></textarea>
<script type="text/javascript">
        window.onload = function()
        {
                CKEDITOR.replace( 'actu-content' );
        };
</script>

and my CSS :

.ActuContent{
    padding:10px 10px 10px 10px;
    color:#416a8b;
    font-size:1.6em;
}

And my CKEditor Config.js file only contains the toolbar config.

CKeditor does not apply the settings of ".ActuContent" to its rendering ...

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

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

发布评论

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

评论(6

北凤男飞 2024-08-21 12:36:52

这个问题的实际最佳答案是:

CKEDITOR.config.contentsCss = '/mycustom.css';
CKEDITOR.replace('myfield');

因为您可能希望在不同的编辑器中拥有不同的样式。如果您像Jalil 所做的那样更改主要content.css,你将无法做到这一点。

The actual best answer to this question would be:

CKEDITOR.config.contentsCss = '/mycustom.css';
CKEDITOR.replace('myfield');

Because you probably would like to have different styles in different editors. If you change the main content.css like Jalil did, you would not be able to do that.

卸妝后依然美 2024-08-21 12:36:52

我找到了一个非常简单的方法来回答我的问题:

CKEditor 目录中的 content.css 文件!

我只需在编辑器中输入我想要应用的样式:

body {
    color: #416a8b;
    font-family: Arial;
    font-size: 18px;
    font-weight: 400;
   text-align: left;
}

仅此而已:-)

I found a very easy way to answer my question :

the content.css file in CKEditor directory !

I only had to put in the style I wanted to be applied inside the Editor :

body {
    color: #416a8b;
    font-family: Arial;
    font-size: 18px;
    font-weight: 400;
   text-align: left;
}

That's all :-)

明明#如月 2024-08-21 12:36:52

请参阅此帖子:

CKEditor:编辑器主体的类或 ID

解决方案nemisj 发布的内容会将类名设置到编辑器可编辑区域的主体上。

您可以在编辑器 onload 函数中执行此操作。在调用 .replace 之前调用此函数。

CKEDITOR.on( 'instanceReady', function( ev )
     {
         CKEDITOR.instances.e1.document.$.body.className = "foo";
     });

See this posting:

CKEditor: Class or ID for editor body

The solution posted by nemisj will set the class name onto the body of the editor's editable area.

You can do this in an editor onload function. Call this before you call .replace.

CKEDITOR.on( 'instanceReady', function( ev )
     {
         CKEDITOR.instances.e1.document.$.body.className = "foo";
     });
情绪 2024-08-21 12:36:52

有时,当我需要动态为 CKEditor 设置某些样式时,例如根据用户设置,我会在编辑器主体上使用 setStyle()setStyles() 函数目的。示例代码:

var editor = CKEDITOR.instances.editor1;
var size = ... // assign size value
editor.document.getBody().setStyle('font-size',size);

另一个示例:

var editor = CKEDITOR.instances.editor1;
var styles = {
    "font-family": "Arial",
    "color": "#333"
};
editor.document.getBody().setStyles(styles);

Sometimes when I need to set some styles to the CKEditor on the fly, for example depending on user settings, I use setStyle() and setStyles() functions on the editor's body object. Sample code:

var editor = CKEDITOR.instances.editor1;
var size = ... // assign size value
editor.document.getBody().setStyle('font-size',size);

Another sample:

var editor = CKEDITOR.instances.editor1;
var styles = {
    "font-family": "Arial",
    "color": "#333"
};
editor.document.getBody().setStyles(styles);
浮光之海 2024-08-21 12:36:52

CKEditor 使用带有普通 HTML 元素的 DIV 来表示您正在编辑的文本。只需查看此 DIV 的内容并编写适当的样式表即可。

当然,只有在渲染之前不修改 CKEditor 的输出时,这才有效。

CKEditor uses a DIV with normal HTML elements to represent the text you're editing. Just have a look at the content of this DIV and write a appropriate style sheet.

Of course, this only works if you don't modify the output of CKEditor before you render it.

他是夢罘是命 2024-08-21 12:36:52

如果您更改 content.css 文件,您可能会发现它已被缓存。在浏览器中刷新它并不是一项简单的任务,因为 CKEDITOR.timestamp 仅添加到 js 文件中。我想出了以下解决方案:

// force to update all plugin files and styles
CKEDITOR.timestamp = '25062014';
CKEDITOR.config.contentsCss = CKEDITOR.config.contentsCss + '?' + CKEDITOR.timestamp;

If you change content.css file you may discover that it's been cached. It's not a trivial task to refresh it in your browser since CKEDITOR.timestamp is added only to js files. I came up with the following solution:

// force to update all plugin files and styles
CKEDITOR.timestamp = '25062014';
CKEDITOR.config.contentsCss = CKEDITOR.config.contentsCss + '?' + CKEDITOR.timestamp;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文