我们如何将有效的 HTML 字符串传递给 html2canvas?
例如
var html = "<html><head></head><body><p>HI</p></body></html>
在 http://html2canvas.hertzen.com/screenshots.html 上完成的方式
Html2canvas 确实很棒,但它的文档却很少。
最佳答案
通常 html2canvas 渲染 DOM 元素而不是 html 代码。但是你可以创建一个临时的 iFrame,让你的 html 在那个 iFrame 中呈现,然后将生成的 DOM 交给 html2canvas。
您可以在 jsfiddle 中找到示例玩,或在这里为了您的方便:
var html_string = "<html><head></head><body><p>HI</p></body></html>";
var iframe=document.createElement('iframe');
document.body.appendChild(iframe);
setTimeout(function(){
var iframedoc=iframe.contentDocument||iframe.contentWindow.document;
iframedoc.body.innerHTML=html_string;
html2canvas(iframedoc.body, {
onrendered: function(canvas) {
document.body.appendChild(canvas);
document.body.removeChild(iframe);
}
});
}, 10);
关于javascript - Html2Canvas 中的 HTML 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16116592/