gpt4 book ai didi

javascript - Html2Canvas 中的 HTML 字符串

转载 作者:太空狗 更新时间:2023-10-29 14:00:55 25 4
gpt4 key购买 nike

我们如何将有效的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com