简体   繁体   English

如何在rails中设置cktext_area的背景颜色/图像

[英]How to set the background-color/image of a cktext_area in rails

I am using Rails 3.0.9 with the ckedit gem. 我正在使用Rails 3.0.9和ckedit gem。

I have the following code in a view. 我在视图中有以下代码。

<%= f.cktext_area :content, :toolbar => 'Reduced', :width => 550, :height => 300 %>

How can I style the resulting editor to set the background-color of the edit area to indicate that validation has failed? 如何设置生成的编辑器样式以设置编辑区域的背景颜色以指示验证失败? For <%=text_area I can set :class=>'error' but I can't figure out the correct approach. 对于<%=text_area我可以设置:class=>'error'但我无法找出正确的方法。

Many thanks for your help - I've searched everywhere for a decent answer this! 非常感谢你的帮助 - 我到处寻找一个体面的答案!

I haven't worked with the ckedit gem (or Rails for that matter), but here are some config settings and methods you can try. 我没有使用过ckedit gem(或Rails),但是这里有一些你可以尝试的配置设置和方法。


You can assign the class (or ID) when you create the instance using a config setting: 使用配置设置创建实例时,可以分配类(或ID):

<%= f.cktext_area :content, :bodyClass => 'yourBodyClass', :bodyId => 'yourBodyId'>

Here's the api page: 这是api页面:
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.bodyClass http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.config.html#.bodyClass


You can use the addCss method when you create the instance. 您可以在创建实例时使用addCss方法。 This is an example that works when creating the editor instance with JavaScript. 这是一个使用JavaScript创建编辑器实例时的示例。

It's possible to put this in your CKEditor config file which might be better in your case because you're creating the CKEditor object using server side code. 可以将它放在您的CKEditor配置文件中,这可能在您的情况下更好,因为您使用服务器端代码创建CKEditor对象。 See below for further details. 请参阅下文了解更多详情。

CKEDITOR.replace( 'content' );
CKEDITOR.instances.content.addCss( 'body { background-color: red; }' );

Here's the API page: 这是API页面:
http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#addCss http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#addCss


You may want to change the color based on circumstances. 您可能希望根据具体情况更改颜色。 If so, you can put a JavaScript or Rails variable in place of the color to make it dynamic. 如果是这样,您可以使用JavaScript或Rails变量代替颜色以使其动态化。

var colorValue = '<%= colorVariable >';

CKEDITOR.instances.content.addCss( 'body { background-color: '+colorValue+'; }' );
CKEDITOR.instances.content.addCss( 'body { background-color: '<%= colorVariable >'; }' );

Not sure about the Rails syntax, but you get the idea. 不确定Rails语法,但你明白了。


Putting the "addCss" method in your CKEditor config file : 将“addCss”方法放在CKEditor配置文件中
You need to put it at the top of the file before "CKEDITOR.editorConfig" is called. 在调用“CKEDITOR.editorConfig”之前,需要将它放在文件的顶部。 It use's the exact same syntax as above and can use a variable to set a dynamic color as described above. 它使用与上面完全相同的语法,并且可以使用变量来设置动态颜色,如上所述。


The "addCss" method has no effect if called after the editor instance is loaded. 如果在加载编辑器实例后调用,则“addCss”方法无效。

If your validation is being done with JavaScript and you need set the background color after everything has loaded (without reloading the page), you can try the following approach. 如果您的验证是使用JavaScript完成的,并且您需要在加载所有内容后设置背景颜色(无需重新加载页面),您可以尝试以下方法。

You can insert the following code into the page that contains the editor instance. 您可以将以下代码插入包含编辑器实例的页面中。 You would call "setIframeBackground()" when validation fails. 验证失败时,您将调用“setIframeBackground()”。 If the color doesn't change, you can hard code it and remove the parameter from the function. 如果颜色没有改变,您可以对其进行硬编码并从函数中删除参数。

The code assumes that you used the "bodyId" config option described above to set the Id of the content area iframe body to "yourBodyId". 该代码假定您使用上述“bodyId”配置选项将内容区域iframe主体的Id设置为“yourBodyId”。

One caveat, I used jQuery to write this code. 有一点需要注意,我用jQuery编写了这段代码。

// Wait for the document ready event
$(document).ready(function(){

  // Wait for the instanceReady event to fire for the "content" instance
  // The instance name is "content"
  CKEDITOR.instances.content.on( 'instanceReady',
    function( instanceReadyEventObj )
    {
      var currentEditorInstance = instanceReadyEventObj.editor;
      var iframeDoc=null;

      // Create the function
      function setIframeBackground( colorVariable )
      {
        // The CKEditor content iframe doesn't have a Name, Id or Class
        // So, we'll assign an ID to the iframe
        // it's inside a table data cell that does have an Id.
        // The Id of the data cell is "cke_contents_content"
        // Note that the instance name is the last part of the Id
        // I'll follow this convention and use an Id of "cke_contents_iframe_content"

        $("#cke_contents_content iframe").attr("id", "cke_contents_iframe_content");

        // Now use the iframe Id to get the iframe document object
        // We'll need this to set the context and access items inside the iframe

        $('#cke_iframe_content').each(
          function(){ iframeDoc=this.contentWindow.document;}
        );

        // Finally we can access the iframe body and set the background color.
        // The Id of the body is set to "yourBodyId".
        // We use the iframe document object (iframeDoc) to set the context.
        // We use the "colorVariable" variable assigned in the function call.

        $('#' + yourBodyId, iframeDoc).css("background-color", colorVariable);
      }
    }
  );
});

Be Well, 好吧,
Joe

Have you tried adding this as an addition parameter? 您是否尝试将其添加为附加参数?

<%= f.cktext_area :content, <params>, :html => {:class => 'error'} %>

You could then use conditional logic to set the correct class name. 然后,您可以使用条件逻辑来设置正确的类名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM