简体   繁体   中英

How to print selected portion of HTML page?

I was trying to print selected <div> tags in HTML. I tried using CSS like this:

<style media="print">
  .onlyscreen {
    display: none;
  }
</style>

<html>
  <body>
    <div class="onlyscreen">
      <p>Hello</p>
      <div>
        <p> Inner tag</p>
      </div>
    </div>
    <input type="submit" value="Print Report" onclick="window.print()">
  </body>
</html>

"Inner tag" is not printed. This is a useful technique, but it fails when there are layered <div> tags.

I also tried this JavaScript:

function printContent(id) {
    str = document.getElementById(id).innerHTML;
    newwin = window.open('', 'printwin', 'left=100,top=100,width=400,height=400');
    newwin.document.write('<HTML>\n<HEAD>\n');
    newwin.document.write('<TITLE>Report</TITLE>\n');
    newwin.document.write('<script>\n');
    newwin.document.write('function chkstate(){\n');
    newwin.document.write('if(document.readyState=="complete"){\n');
    newwin.document.write('window.close()\n');
    newwin.document.write('}\n');
    newwin.document.write('else{\n');
    newwin.document.write('setTimeout("chkstate()",2000)\n');
    newwin.document.write('}\n');
    newwin.document.write('}\n');
    newwin.document.write('function print_win(){\n');
    newwin.document.write('window.print();\n');
    newwin.document.write('chkstate();\n');
    newwin.document.write('}\n');
    newwin.document.write('<\/script>\n');
    newwin.document.write('</HEAD>\n');
    newwin.document.write('<BODY onload="print_win()">\n');
    newwin.document.write(str);
    newwin.document.write('</BODY>\n');
    newwin.document.write('</HTML>\n');
    newwin.document.close();
}

And then calling this function by onclick . This script works fine with one <div> tag but not with mulitple tags. For example

<html>
  <body>
    <div id="print_thistag"> 
      <p>Hello</p>
    </div>
    <div id="print_thistag"> 
      <p>User</p>
    </div>
    <input type="submit" value="Print Report" onclick="printContent('print_thistag')">
  </body>
</html>

Only "Hello" gets printed. What should I do in such cases?

Basically the problem is that your printContent function is designed in such way that it works only with a single element.

You can alter your function, so it will accept not the id of the element but the css class name as in the following sample

function printContent(className) {
    var matchedElements = document.getElementsByClassName(className);
    var str = '';

    for (var i = 0; i < matchedElements.length; i++) {
        var str = str + matchedElements[i].innerHTML;
    }

    var newwin = window.open('', 'printwin', 'left=100,top=100,width=400,height=400');

    newwin.document.write('<HTML>\n<HEAD>\n');
    newwin.document.write('<TITLE>Report</TITLE>\n');
    newwin.document.write('<script>\n');
    newwin.document.write('function chkstate(){\n');
    newwin.document.write('if(document.readyState=="complete"){\n');
    newwin.document.write('window.close()\n');
    newwin.document.write('}\n');
    newwin.document.write('else{\n');
    newwin.document.write('setTimeout("chkstate()",2000)\n');
    newwin.document.write('}\n');
    newwin.document.write('}\n');
    newwin.document.write('function print_win(){\n');
    newwin.document.write('window.print();\n');
    newwin.document.write('chkstate();\n');
    newwin.document.write('}\n');
    newwin.document.write('<\/script>\n');
    newwin.document.write('</HEAD>\n');
    newwin.document.write('<BODY onload="print_win()">\n');
    newwin.document.write(str);
    newwin.document.write('</BODY>\n');
    newwin.document.write('</HTML>\n');
    newwin.document.close();
}

You'll also need to alter html a bit

<html>
  <body>
    <div class="print_thistag"> 
      <p>Hello</p>
    </div>
    <div class="print_thistag"> 
      <p>User</p>
    </div>
    <input type="submit" value="Print Report" onclick="printContent('print_thistag');">
  </body>
</html>

It will work as you expected - aggregate every div content into a single html string which will be printed.

By the way assigning the same id for the multiple elements is not a good practice - the id should be unique and if you want to group elements somehow - you can just add the same class for those elements as in the sample above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM