使用jqprint使用CSS打印特定的DIV

时间:2014-01-11 18:04:54

标签: javascript html css jqprint

我想用css打印一个div。当我申请inline cssexternal and internal css无效时,它与我合作。以下是我的代码,

<script src="jquery.jqprint.0.3.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(function() {
  $("#PrintButton").click( function() {
      $('#divToPrint').jqprint();
      return false;
  });
});
</script>

<style type="text/css">
@media screen,print
  {
  .test {background:#0F0; width:500px; height:200px;}
  }
</style>

<input id="PrintButton" type="button" name="Print" value="Print" />

<div id="divToPrint" class="test">
Print Me, I'm in DIV
</div>

1 个答案:

答案 0 :(得分:3)

我已经尝试过你的代码,它似乎与外部CSS一起正常工作。

<html>
<head>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.jqprint-0.3.js" type="text/javascript"></script>
<link rel="stylesheet" href="test.css">

<script language="javascript" type="text/javascript">
$(function() {
  $("#PrintButton").click( function() {
      $('#divToPrint').jqprint();
      return false;
  });
});
</script>
</head>
<body>
<input id="PrintButton" type="button" name="Print" value="Print" />

<div id="divToPrint" class="test">
Print Me, I'm in DIV
</div>
<body>
</html>

和外部css文件名: test.css

@media screen,print
{
  .test {
            background:#0F0; width:500px; height:200px;
        }
}