【问题标题】:Javascript - Print a full html blockJavascript - 打印一个完整的 html 块
【发布时间】:2018-06-02 10:18:57
【问题描述】:

我有这个 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>";

【问题讨论】:

  • 好像你只贴了一半代码。
  • 欢迎使用 stackoverflow。你应该先尝试自己。当您遇到具体问题时,我们很乐意为您解答!
  • 此问题缺少任何代码来向我们表明您已尝试自己解决此问题。如果您尝试过某些东西,请向我们展示您尝试过的内容、预期输出的示例以及您实际得到的结果。如果您没有尝试过任何事情,则需要在发布之前这样做。我们可以为您的现有代码提供帮助,但我们不会为您编写。请阅读:How to create a Minimal, Complete, and Verifiable example 和 How do I ask a good question?

标签: javascript php html shortcut


【解决方案1】:

您可以通过以下功能打印任何 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 = '&lt;html&gt;&lt;body&gt;&lt;h1&gt;anything you want to print&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;'; print(htmltoprint);

【讨论】:

    猜你喜欢
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    • 2017-06-28
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多