简体   繁体   English

CLeditor使用HTML的“ align”属性。 如何将其更改为CSS“文本对齐”?

[英]CLeditor uses the HTML 'align' attribute. How can I change that to CSS 'text-align'?

I've set up the CLeditor on a site I'm working on. 我已经在正在处理的站点上设置了CLeditor。 Currently I'm setting it up so that as you type and edit within the editor, you can see a live preview of the results just above it, a lot like what you get when typing a StackOverflow question, though much more basic. 目前,我正在对其进行设置,以便您在编辑器中键入和编辑内容时,可以在其上方看到实时预览结果,与输入StackOverflow问题时所获得的效果非常相似,尽管更为基本。

It works by simply copying the inner HTML of the iframe contents to another place on the page. 只需将iframe内容的内部HTML复制到页面上的其他位置即可。 However I've run into an annoying issue. 但是我遇到了一个烦人的问题。 When I use the alignment buttons (left, center, right), it adds the attribute align="right" , for example, to the selected text. 当我使用对齐按钮(左,中,右)时,会将属性align="right"添加到选定的文本中。 While it works in the editor, it does not work on the page itself, probably because that attribute is pretty much obsolete. 虽然在编辑器中可以使用它,但是它不能在页面本身上使用,可能是因为该属性已经过时了。

... ...

I actually figured out how to get around this issue while typing this question. 我实际上想出了如何在输入此问题时解决此问题。 Still, I'll post this question with my solution. 不过,我将在解决方案中发布这个问题。 Plus I have a relevant question to add to this. 另外,我还有一个相关的问题要补充。

Originally I tried applying the following CSS to the page: 最初,我尝试将以下CSS应用于页面:

div[align="right"] {
    text-align:right!important;
    }

This worked for initially loading the data onto the page, but while dynamically changing alignment in the editor, the live preview was not reflecting the changes. 这可以将数据最初加载到页面上,但是在编辑器中动态更改对齐方式时,实时预览无法反映所做的更改。 I thought at first that this was because the styles were applied at load time only. 起初我以为这是因为样式仅在加载时应用。

Well, that was a brain fart because I know better than that. 好吧,那真是个屁,因为我比这更了解。 The real problem was that I was selecting a DIV element and the align attribute isn't necessarily applied to a DIV. 真正的问题是我选择了DIV元素,并且align属性不一定适用于DIV。 Changing div[align="right"] to *[align="right"] works perfectly. 完美地将div[align="right"]更改为*[align="right"]

However, even though I found a workaround for this specific issue, I still can't figure out how the cleditor builds the HTML output for the iframe. 但是,即使我找到了解决此特定问题的方法,也仍然无法弄清cleditor如何为iframe构建HTML输出。 Where does the align attribute come from in the code and how does it know to put it (and all of the other elements/attributes) into the HTML? align属性在代码中来自哪里,如何知道将其(以及所有其他元素/属性)放入HTML? If I had a way of manipulating this, I could simply tell it to use inline CSS for the alignment rather than the deprecated align HTML attribute. 如果我有一种处理方法,我可以简单地告诉它使用内联CSS进行对齐,而不要使用不推荐使用的align HTML属性。 Please note that I do not wish to enable the cleditor's built-in "useCSS" feature. 请注意,我不希望启用cleditor的内置“ useCSS”功能。

Thanks for any information you can share, and please do not downvote this question just because I already solved the initial problem. 感谢您可以分享的任何信息,请不要因为我已经解决了最初的问题而对此问题投了反对票。 I want this to be able to help others if they run into the same issue. 如果其他人遇到相同的问题,我希望它能够为他们提供帮助。 (I'll also post my answer as an answer). (我还将发布我的答案作为答案)。

Applying the following CSS to the live-preview of the page works perfectly: 将以下CSS应用于页面的实时预览效果很好:

*[align="right"] {
  text-align:right!important;
}

Don't forget to do the same for left and center as well. 别忘了对左和中点也做同样的事情。

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

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