如何将非可变文本添加到javascript打印弹出窗口中

How to add non-variable text into javascript print popup?

本文关键字:打印 javascript 窗口 添加 文本      更新时间:2023-09-26

我有一个食谱博客,有一个选项可以只打印博客文章中的食谱,这一切都设置好了并且效果很好,使用此javascript打印正确的东西等:

<script type='text/javascript'>
// Print recipe using iframe
function printRecipe(){
var content = document.getElementById(&quot;recipe&quot;);
var pri = document.getElementById(&quot;ifmcontentstoprint&quot;).contentWindow;
pri.document.open();
pri.document.write(content.innerHTML);
pri.document.close();
pri.focus();
pri.print();
}

我想做的是在每个"打印食谱"的底部添加一个带有网站地址的页脚。这将因食谱而异,但我不希望它显示在原始食谱上,因此它只需要在打印配方时才显示。所以我知道我必须在此代码中的某个地方添加它,但到目前为止还没有运气。

我试图搜索大量论坛,但对必须是一个简单问题没有真正的答案。

请帮忙!谢谢。

在此行之后:

pri.document.write(content.innerHTML);

添加页脚:

pri.document.write("My custom footer! Can include HTML");

最终代码:

<script type='text/javascript'>
// Print recipe using iframe
function printRecipe(){
var content = document.getElementById("recipe");
var pri = document.getElementById("ifmcontentstoprint").contentWindow;
pri.document.open();
pri.document.write(content.innerHTML);
pri.document.write("My custom footer! Can include HTML");
pri.document.close();
pri.focus();
pri.print();
}
</script>