[英]Cloning by javascript an image base64 fails
I need to clone an html block which has some imgs inside, just like this: 我需要克隆一个里面有一些img的html块,就像这样:
<span class="slides_container" id="rotator1">
<div class="slide">
<div class="photoInfo">imagenes 1</div>
<img src="data:image/jpg;base64,/9j...FHFf/Z" width="600" height="400" alt="pic1">
</div>
</div>
<div class="slide">
<div class="photoInfo">imagenes 1</div>
<img src="data:image/jpg;base64,/9j...FHFf/Z" width="600" height="400" alt="pic2">
</div>
</div>
</span>
Then I use this jquery code to clone this block: 然后,我使用此jquery代码克隆此块:
$('#mediaViewerSlides').append(content.clone());
As result, only the first image cloned on its src attr has at the end of the base64 string something like a GET parameter like this: src="...FHFf/Z?133487557212" I can't get know how this value appears or why, obviously this extra data on the base64 string makes the image invalid, and it breaks everything else. 结果,只有在其src attr上克隆的第一个图像在base64字符串的末尾具有类似GET参数的内容,例如:src =“ ... FHFf / Z?133487557212”我不知道该值如何出现或为什么,显然base64字符串上的这些额外数据使图像无效,并破坏了其他所有内容。
I'm not sure why the time-stamp is being added, but instead of cloning the whole object, maybe you can create a new one and set it's src
attribute to the same value: 我不确定为什么要添加时间戳,但是您可以创建一个新对象并将其src
属性设置为相同的值,而不是克隆整个对象:
$('#mediaViewerSlides').append('<img src="' + content.attr('src') + '" width="600" height="400" />');
This assumes that content
is a reference to one of the images with a base64 source. 假定content
是对具有base64源的图像之一的引用。
If the time-stamp is being added to the pre-cloned element then you could strip it from the src
when you create the new element: 如果将时间戳添加到预克隆的元素,则可以在创建新元素时将其从src
剥离:
var source = content.attr('src');
//if the source includes a question-mark, then get everything before the character
if (source.indexOf('?') > -1) {
source = source.split('?')[0];
}
$('#mediaViewerSlides').append('<img src="' + source + '" width="600" height="400" />');
I'm not sure why you HAVE to clone this div. 我不知道你为什么要复制这个div。 Why don't you just keep a reference to the source of the image and add that to a cloned version of the container. 为什么不保留对图像源的引用,然后将其添加到容器的克隆版本中。
I think there is something wrong either with your code or the Base64 string. 我认为您的代码或Base64字符串有问题。
Just found a random image on the web, converted it to base64 here , and cloned it in this fiddle with no such problems, so it's probably not jQuery adding the querystring. 刚刚在网上找到一个随机图像, 将其转换为base64 ,并在这种提琴中将其克隆,没有任何此类问题,因此可能不是jQuery添加了查询字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.