js将网页导出成pdf(js 打印指定div内容)2020-09-02

JavaScript012

js将网页导出成pdf(js 打印指定div内容)2020-09-02,第1张

<input type="button"  id="button" value="点击打印"/>

<div id="div_print">

<p>打印此处内容</p>

</div>

<script type="text/javascript">

function printdiv(printpage){

  var newstr=document.getElementById(printpage).innerHTML

  var oldstr=document.body.innerHTML

  document.body.innerHTML=newstr

  window.print()

  document.body.innerHTML=oldstr

  return false

}

window.onload=function(){

  var bt=document.getElementById("button")

  bt.onclick=function(){printdiv('div_print')}

}

</script>

html, body{

 overflow:visible  

}

打印时另存为pdf模式打印后的内容可复制 默认为Microsoft Print to PDF 模式

文本框内容显示在指定地方:就是一个dom内容转移的操作,使用Jquery获取原生js都很好实现,配合具体js事件实现。具体操作如下: //假定文本框的id='mytext',指定显示区域的id='show' //div/span/p等节点,不是文本元素function fun(){ getEle('s...