我可以从JavaScript访问img标签的数据文件吗? [英] Can I access the datafile of an img tag from JavaScript
本文介绍了我可以从JavaScript访问img标签的数据文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从JavaScript中将作为变量的访问权限作为img标签中的图像加载。
我不想访问它的名字,但是实际的数据。
原因是我希望能够将它复制到变量和从变量中复制,以便我可以在不重新加载的情况下更改图像。
可以这样做吗?如果是这样,怎么做?
注意:我对读的兴趣略高于写数据。
解决方案
//使用AJAX下载图片数据,我使用jQuery
var imageData = $。 ajax({url:MyImage.gif,async:false})。responseText;
//图像数据更新魔术
imageDataChanged = ChangeImage(imageData);
//编码到base64,也许尝试webtoolkit.base64.js库
imageDataEncoded = Base64Encode(imageDataChanged);
//将图像数据写入浏览器(FF似乎支持此功能)
document.write('< img src =data:image / gif; base64,'+ imageDataEncoded + '>');
I want to, from JavaScript, access as a variable the file that is loaded as an image in an img tag.
I don't want to access its name, but the actual data.
The reason for this is that I want to be able to copy it to and from variables so that I can , among other things, change the image without reloading it.
Can this be done? If so, how?
Note: I'm slightly more interested in reading the data than writing it.
解决方案
// Download the image data using AJAX, I'm using jQuery
var imageData = $.ajax({ url: "MyImage.gif", async: false }).responseText;
// Image data updating magic
imageDataChanged = ChangeImage(imageData);
// Encode to base64, maybe try the webtoolkit.base64.js library
imageDataEncoded = Base64Encode(imageDataChanged);
// Write image data out to browser (FF seems to support this)
document.write('<img src="data:image/gif;base64,' + imageDataEncoded + '">');
这篇关于我可以从JavaScript访问img标签的数据文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文