[英]Is it possible to modify this to remove all <img> tags before adding the new one
I have included part of my javascript below and what I want to know is upon success it adds an image to the editor. 我在下面包含了我的JavaScript的一部分,我想知道的是,一旦成功,它就会向编辑器添加图像。 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> </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
is an instance of CKEDITOR.editor
class. oEditor
是CKEDITOR.editor
类的实例。 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();
}
}
I was able to work something out with success. 我能够成功地解决一些问题。 basicall there are two methods in CKEditor that I used.
basicall我在CKEditor中有两种方法。
getData(); 的getData(); setData();
使用setData();
First I got the data from the instance. 首先,我从实例中获取了数据。 I attached it to a temporary created dom-element.
我将其附加到临时创建的dom元素上。 After that I am able to manipulate the content.
之后,我便可以操作内容。 After done the manipulations, I get the html content of the temprorary dom-element and use setData() to place it in the CKEditor.
完成这些操作后,我获得了临时dom元素的html内容,并使用setData()将其放置在CKEditor中。 It may be hacky and I am not sure if this is really the correct way, but it does work.
可能是骇人听闻的,我不确定这是否真的是正确的方法,但是它确实有效。
The instance of the editor: 编辑器的实例:
var forum_signature = CKEDITOR.replace('textAreaName');
var oEditor = CKEDITOR.instances.forum_signature
var jsonImage = 'images/img2.jpg';
var temp = $("<div/>", {
id: "temp"
});
var data = oEditor.getData();
$(data).appendTo(temp);
// you now can traverse the "temp" element in the dom
$(temp).find("img").eq(0).remove();
// You can add html
$(temp).append('<img src=' + jsonImage + ' alt="Image" /><p>Hello World</p>');
var newData = $(temp).html();
$(temp).remove();
oEditor.setData(newData);
你可以这样做:
$(oEditor).children("img").remove();
The following should work. 以下应该工作。 It finds all of the
img
elements inside of your oEditor element and removes them from their parent... 它会在oEditor元素中找到所有
img
元素,并将其从其父级中删除...
if ( oEditor.mode == 'wysiwyg' )
{
// Create an array of img elements.
imgs = oEditor.getElementsByTagName("img");
// Remove each img from its parent node.
for (var i=0; i<imgs.length; i++) {
imgs[i].parentNode.removeChild(imgs[i]);
}
oEditor.insertHtml( '<img src="'+ json['image'] +'" alt="Image" /><p> </p>' );
}
else{
alert( 'You must be in WYSIWYG mode!' );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.