Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在带有Image2插件的CkEditor 4.3中,更改图像src的正确方法是什么?_Javascript_Jquery_Image_Ckeditor - Fatal编程技术网

Javascript 在带有Image2插件的CkEditor 4.3中,更改图像src的正确方法是什么?

Javascript 在带有Image2插件的CkEditor 4.3中,更改图像src的正确方法是什么?,javascript,jquery,image,ckeditor,Javascript,Jquery,Image,Ckeditor,正如标题所说,我使用的是一个基于完整包的版本,其中还包括(build with,以便自动解决每个依赖项) 我需要的是以编程方式(如果需要,也使用jQuery)更改图像的src属性 对于classic,我使用以下代码实现了这一点: var imgToBeReplaced = editor.document.findOne("img#myImg"); imgToBeReplaced.setAttribute("src", newSrc); 因为我需要确保编辑器对象的getData()方法返回正确的

正如标题所说,我使用的是一个基于完整包的版本,其中还包括(build with,以便自动解决每个依赖项)

我需要的是以编程方式(如果需要,也使用jQuery)更改图像的
src
属性

对于classic,我使用以下代码实现了这一点:

var imgToBeReplaced = editor.document.findOne("img#myImg");
imgToBeReplaced.setAttribute("src", newSrc);
因为我需要确保编辑器对象的getData()方法返回正确的数据,所以我还要执行以下操作(阅读更多信息:):

使用时,图像会正确更改,但之后,我无法调整图像大小,也无法访问图像属性(双击图像或右键单击图像打开的关联菜单,因为“属性”选项已不存在)


因此,问题是:如何正确更改
src
(和
data-cke-saved-src
)属性,而不丢失更改图像属性的可能性?

现在使用widgets您只需要担心

  • 获取适当的小部件实例
  • 调用方法
至于这个特殊情况,你需要

// You need to have CKEDITOR.editor instance, here i will pick it from instances property.
var editor = CKEDITOR.instances.editor1,
    // All widgets instances are stored here, we will iterate on top of them.
    widgets = editor.widgets.instances,
    curWidget,
    i;

for ( i in widgets ) {
    curWidget = widgets[ i ];
    // Ensure that image is a part of widget, and src matchs our needs.
    if ( curWidget.definition.name == 'image2' && curWidget.parts.image.getAttribute( 'src' ) == 'assets/image1.jpg' ) {
        // Update src attribute.
        curWidget.setData( 'src', 'http://upload.wikimedia.org/wikipedia/en/1/18/Unicorn-head-circle-2.png' );
    }
}

这将是更新image2src的正确方法-所有内容都将由image2插件本身处理。

您的方法很好,但它只适用于将
curWidget.definition.name=='image2'
更改为
curWidget.definition.name=='image'
;)非常感谢(请在您的回答中进行此编辑)我很高兴该解决方案适合您,但是您确定定义名称吗?我很确定它应该是图像2.:)请仔细检查并让我知道。VitoShadow是正确的-小部件的名称已在4.3.1中更改。我们这样做是为了更容易地用image2.PS替换旧的图像插件。如果要修改聚焦小部件,则可以使用@Reinmar简单地修改此代码-快速附带问题:Image2在全局范围内声明一个
widget
变量,然后在
onShow
函数中将
widget
设置为
this.widget
。我无法在自己的对话框定义中重新创建此对话框。I
console.log
'd
在我的对话框的
onShow
方法中记录此
,并且它不包含
小部件
。知道为什么吗?
// You need to have CKEDITOR.editor instance, here i will pick it from instances property.
var editor = CKEDITOR.instances.editor1,
    // All widgets instances are stored here, we will iterate on top of them.
    widgets = editor.widgets.instances,
    curWidget,
    i;

for ( i in widgets ) {
    curWidget = widgets[ i ];
    // Ensure that image is a part of widget, and src matchs our needs.
    if ( curWidget.definition.name == 'image2' && curWidget.parts.image.getAttribute( 'src' ) == 'assets/image1.jpg' ) {
        // Update src attribute.
        curWidget.setData( 'src', 'http://upload.wikimedia.org/wikipedia/en/1/18/Unicorn-head-circle-2.png' );
    }
}