我有一个包含图像和一些文本的 div。用户可以自定义它。一旦他的修改完成,我想将 div 保存为带有文本的图像。 就像他截屏一样。
我可以在我的 PHP 端保存来自 Base64 字符串的图像,但我无法从 HTML 中获取正确的 Base64。
所以,我想知道如何使用 JavaScript 或 jQuery 从我的 div 中的 HTML 获取 base64。我还可以添加插件。
最佳答案
尝试
var elem = $("div")[0].outerHTML;
var blob = new Blob([elem], {
"type": "text/html"
});
var reader = new FileReader();
reader.onload = function (evt) {
if (evt.target.readyState === 2) {
console.log(
// full data-uri
evt.target.result
// base64 portion of data-uri
, evt.target.result.slice(22, evt.target.result.length));
// window.open(evt.target.result)
};
};
reader.readAsDataURL(blob);
关于javascript - 如何从 HTML 中获取 Base64 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25895007/