html2canvas在Mozila firefox中发生冲突 [英] html2canvas conflict in mozila firefox
本文介绍了html2canvas在Mozila firefox中发生冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用html2canvas
libary.该代码采用div
并将其内容另存为png
.它在opera
和chrome
下可以正常工作,但是当我在firefox
中运行代码时,它不会将div保存为png.单击firefox中的下载按钮没有任何反应.
I am using html2canvas
libary. The code takes a div
and save its content as png
. It works fine with opera
and chrome
but while i run the code in firefox
it does not save the div as png. No action happens on clicking the download button in firefox.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="js/html2canvas.js"></script>
<script src="https://rawgit.com/niklasvh/html2canvas/master/dist/html2canvas.js"></script>
</head>
<body>
<div id="html-content-holder">
Div part to save as png
</div>
<input id="btn-Convert-Html2Image" type="button" value="Download" />
<script>
$('#btn-Convert-Html2Image').click(function() {
html2canvas($('#html-content-holder'), {
onrendered: function(canvas) {
var a = document.createElement('a');
a.href = canvas.toDataURL("image/png")
a.download = 'somefilename.png';
a.click();
}
});
});
</script>
</body>
</html>
推荐答案
在
DOM
中保留一个hidden
锚元素,并更新onrendered
处理程序中该元素的href
属性.
Keep an
hidden
anchor element in theDOM
and update thehref
property of the element insideonrendered
handler.
$('#btn-Convert-Html2Image').click(function() {
html2canvas($('#html-content-holder'), {
onrendered: function(canvas) {
var a = $('#download').get(0);
a.href = canvas.toDataURL("image/png")
a.download = 'somefilename.png';
a.click();
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://rawgit.com/niklasvh/html2canvas/master/dist/html2canvas.js"></script>
<div id="html-content-holder">
Div part to save as png
</div>
<input id="btn-Convert-Html2Image" type="button" value="Download" />
<a href="" id='download'></a>
这篇关于html2canvas在Mozila firefox中发生冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文