locked
Comment faire pour imprimer une partie d'une page web avec JavaScript RRS feed

  • Discussion générale

  • Premiere methode :

     

    Code JavaScript:

    <script language="JavaScript" type="text/JavaScript">
            function doPrint() {
            bdhtml=window.document.body.innerHTML;
            sprnstr="<!--startprint-->";
            eprnstr="<!--endprint-->";
            prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);
            prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
            window.document.body.innerHTML=prnhtml;
            window.print();
            }
    </script>
    

     

    Code  HTML: 

    <!--startprint-->
    This area will print!
    <!--endprint-->
    <br />
    I will not print?
    <input id="btnPrint" type="button" value="Print" onclick="doPrint()" />
    
    

     

     

     

     

    Deuxième méthode:

     

    Code JavaScript: 

    <script language="javascript" type="text/javascript">
        function printdiv(divID)
        {
          var headstr = "<html><head><title></title></head><body>";
          var footstr = "</body>";
          var newstr = document.all.item(divID).innerHTML;
          var oldstr = document.body.innerHTML;
          document.body.innerHTML = headstr+newstr+footstr;
          window.print();
          document.body.innerHTML = oldstr;
          return false;
        }
    </script>

     

     

    Code HTML: 

    <input name="b_print" type="button" onclick="printdiv('divID');" value=" Print " />
    <div id="divID">
    <h1 style="color:green">
    The Div content which you want to print</h1>
    </div>

     

     

    Pour plusieurs informations, visitez la page Foire aux Questions – ASP.NET

     

     

     
     
    Alex Petrescu - MSFT
    mercredi 31 mars 2010 11:39
    Modérateur