繁体   English   中英

具体如何打印 <div> javascript或jquery在新窗口中显示内容?

[英]How to print the specific <div> content in new window using javascript or jquery?

我有一个使用javascript的简单html代码。(如果jquery有可能建议我)

 <script> var myWindow; function openWin() { myWindow = window.open("", "myWindow", "width=200,height=100"); myWindow.document.write(#tobePrint); } function closeWin() { myWindow.close(); } </script> 
 <!DOCTYPE html> <html> <body> <div id="tobePrint">Print the content of this div content in new window.</div> <button onclick="openWin()">Open "myWindow"</button> <button onclick="closeWin()">Close "myWindow"</button> </body> </html> 

我试图在新标签页即将到来的新窗口,我需要新的窗口碰到了tab.The的内容是在div应在新window.So我试图myWindow.document显示的一面。写(#tobePrint); 访问ID并尝试打印,但无法正常工作。

这是工作解决方案:

 var myWindow; function openWin() { myWindow = window.open("myWindow","newwin") var tobePrint = $('#tobePrint').text(); myWindow.document.write('<p>' + tobePrint+ '</p>'); } function closeWin() { myWindow.close(); } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <body> <div id="tobePrint">Print the content of this div content in new window.</div> <button onclick="openWin()">Open "myWindow"</button> <button onclick="closeWin()">Close "myWindow"</button> </body> 

您可以从打开的窗口底部一行的类似代码中获取div内容

 window.opener.document.getElementById('tobePrint').innerHTML

从子窗口获取“ tobePrint” div的内容。 在您的子窗口上编写此代码。

$(document).ready
(
   function () 
  {
      alert(window.parent.document.getElementById('tobePrint').innerHTML);
              }

  );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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