看着使用HTML2Canvas来保存HTML div,我正在寻找使用id“imagesave”保存div我在网上看到了以下代码的示例,但是当我按下载图像时没有任何反应,我怎么能做到这一点工作?
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="html2canvas.js"></script>
<button id="save_image_locally">download img</button>
<div id="imagesave">
<p>testing</p>
<p>testing</p>
</div>
<script>
$('#save_image_locally').click(function(){
html2canvas($('#imagesave'),
{
onrendered: function (canvas) {
var a = document.createElement('a');
a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
a.download = 'somefilename.jpg';
a.click();
}
});
});
</script>
</body>
</html>
指定背景颜色,添加CSS如:
#imagesave {
background:lightblue;
}