javascript - Javascript - 打印一个完整的 html 块
问题描述
我有这个 php 代码用于写下 html 元素列表并使用数组动态更改文本。我想用 Javascript 代码来完成这个,我该怎么做?
<?php
$services = ["Storage", "network", "Virtual Machine", "server","firewall","firewall","users","backup"];
$servicesName = ["Storage", "network", "Virtual Machine", "server","firewall","firewall","users","backup"];
$learnMore - ["Some Brief Description"];
for ($i=0; $i < count($services); $i++) {
$x = $i;
echo "<div class='flexbox-item'>
<div class='learnmore'>
<img src='sourceimages/icons/speech/learnmore.png' alt='LearnMore'>
<div>$learnMore</div>
</div>
<div class='maskshape'></div>
<div class='shape'><img src='sourceimages/icons/services/$services[$i].png' alt='$servicesName[$i]'></div>
<div class='number'><span>0".++$x."</span></div>
<div class='title'><span>$servicesName[$i]</span></div>
</div>";
解决方案
您可以通过以下功能打印任何 html。将 html 字符串作为参数传入其中。
function print(htmlToPass) {
var mywindow = window.open('', 'PRINT', 'height=400,width=600'); //create dummy window
mywindow.document.write(htmlToPass); //write your html in it.
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
return true;
}
前任。调用函数。
var htmltoprint = '<html><body><h1>anything you want to print</h1></body></html>';
print(htmltoprint);
推荐阅读
- javascript - 有没有办法在javascript中制作一个双输入提示框?(举例)
- html - 请问如何将标签和输入放在同一行?
- android - 我可以在编译最低 SDK 21 的同时使用 Navigate Compose 吗?
- python - 屏蔽并连接熊猫中的两个字符串系列
- reactjs - react,react-router-dom中的页面重定向问题
- angular - 是否可以使用 ContentChild 并查询任意组件?
- google-cloud-platform - 在 Google Cloud 上访问 Secret Manager 的问题
- solr - Solr:按子文档字段分组并包含父字段
- database - 如何计算特定表的分区?
- google-cloud-platform - gcp 日志中的执行 ID