是否可以对其进行修改以删除所有< img>标签,然后添加新的 [英] Is it possible to modify this to remove all <img> tags before adding the new one

查看:64
本文介绍了是否可以对其进行修改以删除所有< img>标签,然后添加新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面包含了我的JavaScript的一部分,我想知道的是,一旦成功,它就会向编辑器添加图像。但是,如何使它首先从编辑器中删除所有图像,然后再添加新图像呢?

I have included part of my javascript below and what I want to know is upon success it adds an image to the editor. But how do I make it first remove all images from the editor before adding the new one?

onComplete: function (file, json) {
  $('#upload-image').attr('disabled', false);
  $('.error-upload').remove();
  if (json['success']) {
    alert(json['success']);
    var oEditor = CKEDITOR.instances.forum_signature;
    var value = document.getElementById('forum_signature').value;
    if (oEditor.mode == 'wysiwyg') {
      oEditor.insertHtml('<img src="' + json['image'] + '" alt="Image" /><p>&nbsp;</p>');
    } else {
      alert('You must be in WYSIWYG mode!');
    }
  }
  if (json['error']) {
    $('#upload').after('<span class="error-upload" style="color:red;">' + json['error'] + '</span>');
  }
  if (json['error_size']) {
    $('#upload').after('<span class="error-upload" style="color:red;">' + json['error_size'] + '</span>');
  }
  $('.loading').remove();
}


推荐答案

oEditor CKEDITOR.editor 类的实例。因此,这是正确的解决方案:

oEditor is an instance of CKEDITOR.editor class. Therefore this is the right solution:

if ( oEditor.mode == 'wysiwyg' && oEditor.editable() ) {
    var images = oEditor.editable().getElementsByTag( 'img' );

    while ( images.count() ) {
        var image = images.getItem( 0 ); // This is a live collection.
        // Whether this is a real image, not e.g. anchor icon.
        if ( image.data( 'cke-saved-src' ) )
            image.remove();
    }
}

这篇关于是否可以对其进行修改以删除所有&lt; img&gt;标签,然后添加新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆