文档:https://github.com/tsayen/dom-to-image
<script src="https://cdn.bootcdn.net/ajax/libs/dom-to-image/2.6.0/dom-to-image.js"></script>
代码示例
<html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> table { width: 200px; border-collapse: collapse; border: 1px solid black; } tr { background: green; } </style> <script src="https://cdn.bootcdn.net/ajax/libs/dom-to-image/2.6.0/dom-to-image.js"></script> </head> <body> <input type="button" onclick="saveImage()" value="保存图片" /> <table id="table"> <tr> <td rowspan="3">200</td> <td>200</td> <td>300</td> </tr> <tr> <td rowspan="2">500</td> <td>600</td> </tr> <tr> <td>600</td> </tr> </table> <script> function saveImage() { domtoimage .toPng(document.getElementById("table")) .then(function (dataUrl) { var link = document.createElement("a"); link.download = "my-image-name.jpeg"; link.href = dataUrl; link.click(); }); } </script> </body> </html>