【问题标题】:Add inline CSS in the image using TinyMCE使用 TinyMCE 在图像中添加内联 CSS
【发布时间】:2016-05-25 12:05:33
【问题描述】:

我使用Tiny MCE 4 在我的网站上发帖的用户。它工作得很好,但问题是当用户将图像放入 Tiny MCE 并更改其大小时,它会生成以下行:

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

结果是没有响应的图片。如何配置 Tiny MCE 以在图像上添加以下内联 css 属性?

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

【问题讨论】:

  • 一张图片如何同时具有固定尺寸和响应性?
  • 如果您想在 tinymce 中应用样式,您可以在每个图像中应用样式,只需在您的 tinymce.init 函数中设置 image_advtab: true。通过上述设置,您可以在插入/编辑图像的高级设置选项卡中设置样式

标签: javascript css tinymce tinymce-4


【解决方案1】:

为什么不简单地创建一个 CSS 规则,将所需的属性添加到特定容器的所有 img 元素。 例如,在 container 类的 div 元素中,您可以定义以下 CSS 选择器:

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

这是一个简单页面上的结果示例。您可以看到黄色图像没有改变,但容器 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>

【讨论】:

    【解决方案2】:

    使用 '!important' 添加内联样式,并且任何图像都不会改变大小。

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

    【讨论】:

      【解决方案3】:

      您可以使用 javascript(使用 jQuery)来处理这个问题(无需重新配置 tinymce)。您只需将其包含在显示图像的页面中即可。

      $(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');
          });
      });
      

      【讨论】:

        【解决方案4】:

        如果您将image_advtab 设置为true,您可以在Tinymce 中设置style 属性值,如下例所示。通过应用它,您将在插入/编辑图像中获得一个额外的选项卡并设置您想要的样式。

        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' }
              ]
             });
        

        【讨论】:

        • 这是管理员设置吗?还是用户必须为添加的每个图像手动设置这些属性?
        • @ArnaudDevelay 这是供用户设置他/她在图像中想要的样式。如果您想对所有图像应用相同的样式,我看到您对该方向有响应(答案)。我只是添加了一个没有写的答案。
        猜你喜欢
        • 1970-01-01
        • 2017-08-25
        • 1970-01-01
        • 2011-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        相关资源
        最近更新 更多