在 html 网页上添加“打印”按钮,单击该按钮后,将打印整个网页。这是在网页中添加的相当简单的功能,可以使用一些 html 元素和纯 javascript 来添加。
因此,让我们讨论一下这样做的方法。
方法首先,在 html dom 中添加 73a3ca28445b1c625f2086a50cb8c7df 标签。
将其 type 属性分配给“button”并为其赋予一些值。
然后为将使用 javascript 处理的输入分配一个“onclick”处理程序。
处理输入标签点击事件的函数如下所示 -
const handleprint = () => { window.print();}
此方法的完整代码为 -
示例<!doctype html><html lang=en-us><head> <title> adding print button </title></head> <body style=color: black;> <h1>hello world!</h1> <p>welcome to my website</p> <p> this website is built using plain js and html. </p> <p> okay bye!!! </p> <input value='print' type='button' onclick='handleprint()' /> <script type=text/javascript> const handleprint = () => { var actcontents = document.body.innerhtml; document.body.innerhtml = actcontents; window.print(); } </script> </body></html>
以上就是如何添加一个按钮来打印一个html页面?的详细内容。