您的位置:首页 > Web前端 > JavaScript

JavaScript学习

2013-12-20 00:00 225 查看
js 打印指定区域
 

方法一:

<script language="javascript" type="text/javascript">

    function printpage(myDiv){
    //var newstr = document.all.item(myDiv).innerHTML;
    var newstr = document.getElementById(myDiv).innerHTML;
     alert(newstr);
    var oldstr = document.body.innerHTML;
    document.body.innerHTML = newstr;
    window.print();
    document.body.innerHTML = oldstr;
    return false;
    } 
    </script>

   <div id="myDiv"  >说明:打印整个div区域的内容,如果包含按钮,则按钮也会打印出来!
    <div>
      打印内容打印内容打印内容打印内容打印内容打印内容打印内容打印内容
    adfhioasdhfiohasdofihosdhfosdhfiosdhfiosdfhsdfhsdifsidfiosdfhosdhf
        </div>
    <input type="button" id="bt" onclick="javascript:printpage('myDiv')"   value="打印" />
    </div>

方法二:

<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>

<div>
    <!--startprint-->说明:这是开始打印的位置
    打印内容打印内容打印内容打印内容打印内容打印内容打印内容打印内容
    adfhioasdhfiohasdofihosdhfosdhfiosdhfiosdfhsdfhsdifsidfiosdfhosdhf
    <!--endprint-->说明:这是结束打印的位置
    </div>
    <input type="button" id="bt" onclick="javascript:doPrint()"   value="打印" />
    </div>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: