简体   繁体   English

“打印页面”按钮不打印

[英]“Print page” button does not print

I am trying to use JavaScript to put a “print page” button in an HTML page.我正在尝试使用 JavaScript 在 HTML 页面中放置“打印页面”按钮。 It looks great and clicks but will not print.它看起来很棒,可以点击,但不会打印。 I get 3 errors in the js file.我在 js 文件中收到 3 个错误。 They all say document is undefined他们都说文档未定义

 (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 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">

This snippet certainly run.这个片段肯定会运行。 So you must have a broken JS somewhere that break your code in the middle.因此,您必须在某个地方有一个损坏的 JS,它会在中间破坏您的代码。 Like including a jQuery lib will override your $ .就像包含一个 jQuery 库一样会覆盖你的$ We would need more information.我们需要更多信息。 Try to do some debugging to see if your code reach that function.尝试做一些调试,看看你的代码是否达到了 function。

At very least, try simply console.log($("printButton")) to see if it found the element.至少,尝试简单的console.log($("printButton"))看看它是否找到了元素。 If the element is loaded using Ajax, then it's a another story.如果元素是使用 Ajax 加载的,那就另当别论了。

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

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