CKEDITOR – 防止将图像尺寸添加为CSS样式

前端之家收集整理的这篇文章主要介绍了CKEDITOR – 防止将图像尺寸添加为CSS样式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何防止CKEDITOR添加图像尺寸作为样式?

而不是这样:

@H_502_4@<img src="image.jpg" style="height:100px; width:100px;">

我要这个

@H_502_4@<img src="image.jpg" height="100px" width="100px">

解决方法

你可以通过在CKEditor的config.js中插入这个代码解决这个问题 @H_502_4@CKEDITOR.on('instanceReady',function (ev) { // Ends self closing tags the HTML4 way,like <br>. ev.editor.dataProcessor.htmlFilter.addRules( { elements: { $: function (element) { // Output dimensions of images as width and height if (element.name == 'img') { var style = element.attributes.style; if (style) { // Get the width from the style. var match = /(?:^|\s)width\s*:\s*(\d+)px/i.exec(style),width = match && match[1]; // Get the height from the style. match = /(?:^|\s)height\s*:\s*(\d+)px/i.exec(style); var height = match && match[1]; if (width) { element.attributes.style = element.attributes.style.replace(/(?:^|\s)width\s*:\s*(\d+)px;?/i,''); element.attributes.width = width; } if (height) { element.attributes.style = element.attributes.style.replace(/(?:^|\s)height\s*:\s*(\d+)px;?/i,''); element.attributes.height = height; } } } if (!element.attributes.style) delete element.attributes.style; return element; } } }); });

猜你在找的CSS相关文章