简体   繁体   English

使用TinyMCE在图像中添加内联CSS

[英]Add inline CSS in the image using TinyMCE

I'm using Tiny MCE 4 for users posting on my website. 我正在将Tiny MCE 4用于在我的网站上发布的用户。 It works very well, but the problem is that when a users puts an image in Tiny MCE, and changes its size, it generates the following line: 它工作得很好,但是问题是,当用户将图像放入Tiny MCE中并更改其大小时,它将生成以下行:

<img src="source/imagem.jpg?14641" alt="" width="550" height="838">

The result is a non responsive picture. 结果是无响应的图片。 How can I configure Tiny MCE to add the following inline css attributes on the image ? 如何配置Tiny MCE在图像上添加以下内联CSS属性?

<img src="source/imagem.jpg?14641" alt="" width="550" height="838" style="
    width: 100%; max-width: 550px; height: auto;">

Why not simply creating a CSS rule that adds the required attributes to all img elements of a particular container. 为什么不简单地创建一个CSS规则,将必需的属性添加到特定容器的所有img元素。 For example, in a div element of class container , you can define the following CSS selector: 例如,在类containerdiv元素中,可以定义以下CSS选择器:

.container img {
    max-width:100%;
    height:auto;
}

Here is a sample of the result on a simple page. 这是一个简单页面上的结果示例。 You can see that the yellow image is unchanged but all images contained in the container div children elements are resized (here the green and blue images). 您可以看到黄色图像没有变化,但是容器div子元素中包含的所有图像均已调整大小(此处为绿色和蓝色图像)。

 .container img { max-width:100%; height:auto; } 
 <img src="http://placehold.it/400x400/EEE04A/ffffff" /> <div class="container" style="width:150px;"> <img src="http://placehold.it/800x300/5cb85c/ffffff" /> <div> <img src="http://placehold.it/800x600/5bc0de/ffffff" /> </div> </div> 

Add inline styles with '!important' and any image don't change size. 用'!important'添加内联样式,任何图像都不会改变大小。

 .tiny-mce-wrapper img { width: 100% !important; max-width:100% !important; height:auto !important; } 

You can use javascript (with jQuery) to handle this (without reconfiguring tinymce). 您可以使用javascript(使用jQuery)来处理此问题(而无需重新配置tinymce)。 You just have to include this into the page that is showing the images. 您只需要将其包含在显示图像的页面中即可。

$(document).ready(function () {

    $('.Container').find('img').each(function () {

        var _this = $(this);

        _this.css({
            'width': _this.width,
            'height': _this.height
        });

        _this.removeAttr('width');
        _this.removeAttr('height');
    });
});

You can set style attribute values in Tinymce if you have image_advtab set to true as in the below example. 如果将image_advtab设置为true ,则可以在Tinymce中设置style属性值,如以下示例所示。 By applying this you will get an extra tab in your insert/edit image and set the style you want. 通过应用此功能,您将在插入/编辑图像中获得一个额外的标签,并设置所需的样式。

Tinymce example Tinymce示例

 tinymce.init({
      selector: 'textarea',
      height: 500,
      theme: 'modern',
      plugins: [
        'advlist autolink lists link image charmap print preview hr anchor pagebreak',
        'searchreplace wordcount visualblocks visualchars code fullscreen',
        'insertdatetime media nonbreaking save table contextmenu directionality',
        'emoticons template paste textcolor colorpicker textpattern imagetools'
      ],
      toolbar1: 'insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image',
      toolbar2: 'print preview media | forecolor backcolor emoticons',
      image_advtab: true,
      templates: [
        { title: 'Test template 1', content: 'Test 1' },
        { title: 'Test template 2', content: 'Test 2' }
      ]
     });

在此处输入图片说明

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

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