javascript - 转换为 Canvas 时如何打开详细信息标签

标签 javascript html canvas html5-canvas

在我的 html 页面中,有一个 div 元素,其中包含 2-3 个详细信息标签(默认情况下,所有详细信息标签都将处于折叠模式),其中包含表格。我正在尝试将此 html div 内容转换为 Canvas 。我正在用那个 Canvas 创建一个 pdf 文件。

下面是代码

enter code here
            <div id="result">
                    <details style="margin-top: 10px;">
                        <summary>
                            <b>Name</b>
                        </summary>
                        <table id="table1" border="0" width="100%">
                            <tr>
                                <td width="35%">Subject</td>
                            </tr>
                            <tr>
                                <td>Issues</td>
                            </tr>
                            <tr>
                                <td>Issues</td>
                            </tr>
                        </table>
                    </details>
                    <details style="margin-top: 10px;">
                        <summary>
                            <b>Name</b>
                        </summary>
                        <table id="table2" border="0" width="100%">
                            <tr>
                                <td width="35%">Subject</td>
                            </tr>
                            <tr>
                                <td>Issues</td>
                            </tr>
                            <tr>
                                <td>From</td>
                            </tr>
                        </table>
                    </details>
                    <details style="margin-top: 10px;">
                        <summary>
                            <b>Name</b>
                        </summary>
                        <table id="table3" border="0" width="100%">
                            <tr>
                                <td width="35%">Subject</td>
                            </tr>
                            <tr>
                                <td>Issues</td>
                            </tr>
                            <tr>
                                <td>From</td>
                            </tr>
                        </table>
                    </details>

            </div>

Canvas 转换和 pdf 创建代码

enter code here
            $("body").on("click", "#btnExport", function () {
                    html2canvas($('#Result')[0], {
                        onrendered: function (canvas) {
                            var data = canvas.toDataURL();
                            var docDefinition = {
                                content: [{
                                    image: data,
                                    width: 500
                                }]
                            };
                            pdfMake.createPdf(docDefinition).download("Table.pdf");
                        }
                    });
                });

但问题是我能够创建一个带有关闭详细信息标签的 pdf。我希望将整个内容保存在 pdf 中。

转换为 Canvas 时有什么方法可以更改展开此详细信息吗?

最佳答案

<details open>

<details>有一个 [open]属性。

演示 1(JavaScript):

- 收集所有<details>转换为 NodeList,然后将其转换为数组。
- 使用 for...of 迭代新数组循环。
- 在每次迭代中,它都会切换每个 <details> [open]属性为 .toggleAttribute()方法。

<小时/>

演示 2 (jQuery):

- 于 .each() <details>
- 使其[open]属性 true使用.attr()方法。

<小时/>

在调用 html2canvas() 之前运行任一版本当然。

<小时/>

演示 1

const detailsArray = Array.from(document.querySelectorAll('details'));

for (let details of detailsArray) {
  details.toggleAttribute('open');
}
details {
  margin: 10px;
  padding: 5px;
  border: 5px ridge #bbb;
}

summary {
  border: 3px inset #333;
  padding: 0 5px;
  cursor: pointer;
}
<details>
  <summary>Read More...</summary>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</details>


<details>
  <summary>Read More...</summary>
  Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
</details>


<details>
  <summary>Read More...</summary>
  Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</details>

<小时/>

演示 2

$('details').each(function() {
  $(this).attr('open', true);
});
details {
  margin: 10px;
  padding: 5px;
  border: 5px ridge #bbb;
}

summary {
  border: 3px inset #333;
  padding: 0 5px;
  cursor: pointer;
}
<details>
  <summary>Read More...</summary>
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</details>


<details>
  <summary>Read More...</summary>
  Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
</details>


<details>
  <summary>Read More...</summary>
  Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</details>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

关于javascript - 转换为 Canvas 时如何打开详细信息标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54656576/

相关文章:

javascript - 如何检查 ul 标签的 li 中是否正在播放任何 YouTube 视频,然后相应地执行一些操作

javascript - 服务 vs Controller vs 外部 js 将常用方法放入 AngularJS 中

jquery - 文件上传两次到不同的服务器?

javascript - 使用canvas在javascript中绘制一个正方形

java - 椭圆与圆

php - 在 php 中保存 Canvas 但在图像文件中没有数据

javascript - 添加 onclick 属性以使用 JavaScript 输入

javascript - 循环遍历表并保存其内容

javascript - 目标容器不是 ReactJS 中的 DOM 元素

html - IE中的元素重叠问题