简体   繁体   English

网页打印按钮不起作用

[英]Webpage Print button not working

I am very new to jQuery and html, and I am trying to make a print button on my webpage. 我是jQuery和html的新手,我正在尝试在网页上创建一个打印按钮。 I get the little print image to show up, but clicking on the image does nothing. 我得到小的打印图像显示出来,但是单击图像没有任何作用。 My cursor doesn't even change when my mouse hovers over the image. 当鼠标悬停在图像上时,光标甚至没有变化。 I have a jQuery function that is supposed to handle the logic for printing. 我有一个jQuery函数,应该可以处理打印逻辑。 Can anyone see what I am doing wrong? 谁能看到我在做什么错?

Here is the code that I have so far: 这是我到目前为止的代码:

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link rel="stylesheet" href="PoliticsHTML_CSS.css" type="text/css"/>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
<title>Display HTML Table</title>

<script>
$('.printMe').click(function(){
     window.print();
});
</script>

</head>
<body>

<?php
  error_reporting(E_ALL); 
  ini_set('display_errors', 1);

$con=mysqli_connect("******","*****","***", "***");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


$sql = "SELECT * FROM PoliticsPoll";
$result = mysqli_query($con, $sql);

echo "<table border='1' class='pure-table-bordered'>
<tr>
<th>ID</th>
<th>Politics Poll Question 1</th>
<th>Politics Poll Question 2</th>
<th>Politics Poll Question 3</th>
<th>Politics Poll Question 4</th>
<th>Politics Poll Question 5</th>
<th>Politics Poll Question 6</th>
<th>Politics Poll Question 7</th>
<th>Politics Poll Question 8</th>
<th>Politics Poll Question 9</th>
<th>Politics Poll Question 10</th>
</tr>";

echo "</table>";

mysqli_close($con);
?>

<img src="print.png" class="printMe" alt="Print" title="Print"></a>

</body>
</html>

you add the "click"-handler to a DOM-element that doesn't exists (now). 您可以将“ click”处理程序添加到尚不存在的DOM元素中。 move your <script> after the <img> and before </body> . <script>移到<img>之后</body>之前。

Most likely it is because the printMe element doesn't exist yet, you need to wait until it has loaded: 很有可能是因为printMe元素尚不存在,您需要等待它加载完毕:

$(document).ready(function() {
   $('.printMe').click(function(){
     window.print();
  });
});

That tells it to wait until the document has loaded to bind the event handler. 这告诉它要等到文档加载完成以绑定事件处理程序。

You don't realy need to use Jquery for this, try changing your print button to this: 您确实不需要为此使用Jquery,请尝试将打印按钮更改为此:

<script>
    function Print () {
        window.print()
    }
</script>

<img src="print.png" class="printMe" onClick="Print()" alt="Print" title="Print"></a>

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

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