繁体   English   中英

CKEditor在插入元素之前获取图像的宽度和高度(预加载)

[英]CKEditor Getting image width and height before inserting element (preloading)

在实际将图像插入编辑器之前,是否可以获取图像的宽度和高度?

我有以下代码,但宽度和高度始终返回0

var imageElement = editor.document.createElement('img');
imageElement.setAttribute('src', imageSource);

var width = imageElement.$.width;
var height = imageElement.$.height;

if (width > 0) {
    this.imageElement.setAttribute('width', width);
}
if (height > 0) {
    this.imageElement.setAttribute('height', height);
}

editor.insertElement(imageElement);

帮助将不胜感激

我通过手动预加载图像来解决此问题,但是我不知道这是否是CKEditor的方法来实现。

码:

var imageElement = editor.document.createElement('img');
imageElement.setAttribute('src', imageSource);

function setWidthAndHeight() {
    if (this.width > 0) {
        imageElement.setAttribute('width', this.width);
    }
    if (this.height > 0) {
        imageElement.setAttribute('height', this.height);
    }
    return true;
}

var tempImage = new Image();
tempImage.src = imageSource;
tempImage.onload = setWidthAndHeight;

editor.insertElement(imageElement);

暂无
暂无

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

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