【问题标题】:How to remove pop-up option in printing如何删除打印中的弹出选项
【发布时间】:2019-04-09 05:02:05
【问题描述】:

我想在打印 textarea 的内容时从此脚本中删除弹出选项,以便打印页面将在 textarea 的同一页面中,因为我添加了 css,因此打印的页面不会有链接或日期“@page { size: auto; margin: 0mm; }" 但是当它在单独的页面中弹出时会显示链接和日期

<html >
<head>
<title></title>
<!-- script print button -->
 <script type="text/javascript">
      function printTextArea() {
	
        childWindow = window.open('','childWindow','location=yes, menubar=yes, toolbar=yes');
        childWindow.document.open();
        childWindow.document.write('<html><head></head><body dir="rtl">');
        childWindow.document.write(document.getElementById('targetTextArea').value.replace(/\n/gi,'<br/>'));
        childWindow.document.write('</body></html>');
        childWindow.print();
        childWindow.document.close();
        childWindow.close();
      }
    </script>
<style type="text/css">
@page { size: auto;  margin: 0mm; }
textarea {
direction: rtl; 
 background-color: white;
 font-size: 1em;
 font-weight: bold;
 font-family: Verdana, Arial, Helvetica, sans-serif;
 border: 1px solid #00acee;
 resize: none;
}
input[type=button] {
    background-color: #00acee;
    border: none;
    color: white;
    padding: 16px 32px;
    text-decoration: none;
    margin: 4px 2px;
    cursor: pointer;
}
</style>
</head>

<body>
  <p>
    <TEXTAREA name="thetext" rows="20" cols="80"id="targetTextArea" placeholder="قم بنسخ و لصق الطلب لملأه و التعديل عليه و طباعته بالزر أسفله ......"></TEXTAREA>
   </p>
   <!-- print button -->
  <center> <input type="button" onclick="printTextArea()" value="طباعة"/></center>
</body>
</html>

【问题讨论】:

  • 请添加一个 plunker 来展示您想要实现的目标。
  • 当您单击按钮中的按钮时,会打印 textarea 中的文本,但问题是在弹出的单独页面中打印而不是在同一页面中打印

标签: javascript css printing textarea editor


【解决方案1】:

发生这种情况是因为您在 printTextArea() 中打开了一个新窗口。

当使用 window.open() 方法打开窗口时,您可以使用目标窗口中的此属性来返回源(父)窗口的详细信息。 您可以在 w3schools 阅读更多相关信息。 W3School Window Opener

      function printTextArea() {

        childWindow = window.open('','childWindow','location=yes, menubar=yes, toolbar=yes');
        childWindow.document.open();
        childWindow.document.write('<html><head></head><body dir="rtl">');
        childWindow.document.write(document.getElementById('targetTextArea').value.replace(/\n/gi,'<br/>'));
        childWindow.document.write('</body></html>');
        childWindow.print();
        childWindow.document.close();
        childWindow.close();
      }

您应该通过 id 选择 textarea 并使用 .innerHTML 更新它的 html,而不是打开新窗口。例如:

document.getElementById("demo").innerHTML = "Paragraph changed!";

【讨论】:

  • 我刚开始编码,我不是那么好,请问我应该怎么做
  • @HAMID,你应该这样做: document.getElementById('targetTextArea').innerHTML = "你的新值或 HTML";您的打印页面可以从您获得的任何地方获得 innerHTML 值(我认为在这种情况下它是 textarea)
猜你喜欢
  • 2017-12-16
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多