繁体   English   中英

“打印页面”按钮不打印

[英]“Print page” button does not print

我正在尝试使用 JavaScript 在 HTML 页面中放置“打印页面”按钮。 它看起来很棒,可以点击,但不会打印。 我在 js 文件中收到 3 个错误。 他们都说文档未定义

 (function() { 'use strict'; }()) var $ = function(id) { return document.getElementById(id); } var printPage = function() { window.print(); } window.onload = function() { $("printButton").onclick = printPage; }
 <input type="button" id="printButton" value="Print Page">

html

<head>
       <script src="printPage.js"></script> 

</head>
<body>  
        <input type="button" id="printButton" value="Print Page">
<body>

 (function() { 'use strict'; }()) var $ = function(id) { return document.getElementById(id); } var printPage = function() { window.print(); } window.onload = function() { $("printButton").onclick = printPage; }
 <input type="button" id="printButton" value="Print Page">

这个片段肯定会运行。 因此,您必须在某个地方有一个损坏的 JS,它会在中间破坏您的代码。 就像包含一个 jQuery 库一样会覆盖你的$ 我们需要更多信息。 尝试做一些调试,看看你的代码是否达到了 function。

至少,尝试简单的console.log($("printButton"))看看它是否找到了元素。 如果元素是使用 Ajax 加载的,那就另当别论了。

暂无
暂无

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

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