所以我在我的php网页上创建了多个手风琴。我想在每个按钮上都有一个打印按钮,以便用户以我的格式打印出来。这是代码:
PHP:
for($i=0; $i<$length; $i++) //Length is usually between 3-6
{
echo "<div id='nestedAccordion'>";
echo "<h2><table><thead></thead>";
echo "<tr><td>". $data[$i][0] . "</td>";
echo "</tr><tr><td>" . $data[$i][1] . "<td>";
echo "</tr>";
echo "</table>";
echo "</h2>";
echo "<div>";
echo "<h3>Subcategory1</h3>" // Couple sub category
echo "</div>";
echo "<div id='printableArea'>";
echo "<p>AD: ".$data[$i][0]."</p>";
echo "<p>Descripton: ".$data[$i][1]."</p>";
echo "</div>";
echo "<input style='float: right' type='button' onclick="."printDiv('printableArea')"." value='Print' />";
}
使用Javascript:
$(document).ready(function () {
var parentDivs = $('#nestedAccordion div'),
childDivs = $('#nestedAccordion h3').siblings('div');
$('#nestedAccordion h2').click(function () {
parentDivs.slideUp();
if ($(this).next().is(':hidden')) {
$(this).next().slideDown();
} else {
$(this).next().slideUp();
}
});
$('#nestedAccordion h3').click(function () {
childDivs.slideUp();
if ($(this).next().is(':hidden')) {
$(this).next().slideDown();
} else {
$(this).next().slideUp();
}
});
});
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
CSS:
#printableArea{
display: none
}
此代码有效,因为我在每个手风琴上都有打印按钮。但是当我点击我的任何打印按钮,并打开打印预览时,数据仅来自第一个手风琴。我该如何解决这个问题,所以打印按钮只有手风琴的数据。