我有一个带有按钮的html页面,该按钮允许使用以下代码打印特定内容:
<div class="below_movie_left" id="printableArea">
My printing contents
</div>
<input type="button" class="submit_button" onclick="printDiv('printableArea')" value="Print" style="float:right;" />
<script>
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
嗯,它完美地打印了我的特定内容。但是,现在我想使用此按钮打印另一页。另一个页面名称是finalprinting.php
所以现在我想,当我按下打印按钮时,它应该是打印finalprinting.php
页面而不是我页面的现有内容。
答案 0 :(得分:0)
由于我不熟悉jQuery,我会在没有它的情况下尝试这样做
function printDiv(divName){
var xmlhttp = new XMLHttpRequest();
var originalContents = document.body.innerHTML;
xmlhttp.open('GET', '/finalprinting.php', false);
xmlhttp.send(null);
if(xmlhttp.status == 200) {
document.body.innerHTML = xmlhttp.responseText;
window.print();
document.body.innerHTML = originalContents;;
}
}
答案 1 :(得分:0)
您需要使用AJAX。
<input type="button" id="loadpage" value="Print">
<script>
document.getElementById( "loadpage" ).addEventListener( "click", function(){
var request = new XMLHttpRequest();
request.open("GET", "finalprinting.php", true);
request.onload = function(){
if (request.status >= 200 && request.status < 400){
// We got the data, now we can set the body's innerhtml
document.body.innerHTML = request.responseText;
} else {
// We reached our target server, but it returned an error
}
};
request.onerror = function() {
// There was a connection error of some sort
};
request.send();
});
</script>