可以在javascript中定义的函数中打印页面吗?

可以在javascript中定义的函数中打印页面吗?,javascript,Javascript,我一直在使用文档打印机,需要定义一个函数来获取一个值,用该值替换文本框,隐藏打印按钮,并打印页面。我正在尝试此功能: <!DOCTYPE HTML> <HTML> <head> <title>KB documents</title> </head> <body> <style> body { text-align:center; } </style> <div id="hidde

我一直在使用文档打印机,需要定义一个函数来获取一个值,用该值替换文本框,隐藏打印按钮,并打印页面。我正在尝试此功能:

<!DOCTYPE HTML>
<HTML>
<head>
<title>KB documents</title>
</head>
<body>
<style>
body {
  text-align:center;
}
</style>
<div id="hidden">
<textarea cols="125" rows="30" id="value"></textarea>
</div>
<button id="button" onclick="print()">print document</button>
<script>
 function print() {
  var value = document.getElementById("value").value;
  document.getElementById("hidden").innerHTML = "<p>" + value + "</p>";
  document.getElementById("button").style.display = "none";
  window.print()
}
</script>
</body>
</html>

知识库文件
身体{
文本对齐:居中;
}
打印文档
函数打印(){
var value=document.getElementById(“value”).value;
document.getElementById(“隐藏”).innerHTML=“”+value+”

”; document.getElementById(“按钮”).style.display=“无”; window.print() }
它除了打印最重要的页面外,什么都可以做。我犯错误了吗


谢谢您的帮助。

顶层的
功能print
正在覆盖内置的
窗口。print
。使用不同的变量名,以便不会重新分配
window.print

<button id="button" onclick="doPrint()">print document</button>
<script>
 function doPrint() {
  var value = document.getElementById("value").value;
  document.getElementById("hidden").innerHTML = "<p>" + value + "</p>";
  document.getElementById("button").style.display = "none";
  window.print()
}
document.querySelector('#button').addEventListener('click', () => {
  var value = document.getElementById("value").value;
  document.getElementById("hidden").innerHTML = "<p>" + value + "</p>";
  document.getElementById("button").style.display = "none";
  window.print()
});